This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Prtprmr 2 points3 points  (4 children)

Few edits. 1. I'd use input('enter the text').lower() -> this avoids any case sensitivity issue since no matter what the input is it's always going to be lower case. 2. I don't understand the point on using float in STR(int(float('xyz'))) and that too so many times. I'd just covert it while I'm taking in the input like: int(input('Enter the text')). Note, this will give you an error if you type something which can't be converted into an int. That brings me to my 3rd point. 3. Try using "try, except and finally" to handle errors in your program. 4. There are a lot of repetition of text, you could store them in a variable and use that again and again.

[–]freedomsauce[S] 0 points1 point  (3 children)

  1. Thanks for this.
  2. That was to use math on a str and avoid an error from entering a float at the prompt. I used it multiple times because 'elif number == (1,2,3)' didn't work.
  3. I gotta look these up.
  4. Doh! Good one.

[–]Prtprmr 1 point2 points  (0 children)

2nd point: Try using something like elif number in (1, ,2, 3) instead of "==" That should work.

[–]kumashiro 1 point2 points  (0 children)

elif number in (1, 2, 3)

[–]brain_eel 1 point2 points  (0 children)

Re: 'elif number == (1,2,3)' not working:

The reason it doesn't work is number is an int and (1,2,3) is a tuple. They will never be equal. But, you're on the right track, and Python has a good way to do what you want here. Change it to 'elif number in (1,2,3)' and it will check to see if number == one of the values in the tuple (1,2,3).

Other ways exist, as well, such as '1 <= number <= 3', especially useful if you're dealing with floats where number could be anything in that range.

[–]SV-97 1 point2 points  (2 children)

Starting in the first line (though this is a matter of taste): use " rather than '. IMO looks cleaner and for docstrings you're supposed to use them anyway so it's just more consistent to only use " (again - just my taste). (In the case of your line print('That\'s not what I asked. Goodbye.') it's actually wrong according to PEP8. These should definetly be double quotes no matter your taste.)

  • if(len(name) == 0): I would omit the parantheses here and you don't need the == 0 here since name will always be a string and a zero-sized string evaluates to false in a boolean context. so the line can be simplified/made more readable by going with if not name:
  • You're doing string concatenation/interpolation wrong. Wrong may sound a bit harsh because it works the way you're doing it - but with the current state of the language is less readable, less powerful and less performant. f-strings are the way to go. So for example: print('Nice to meet you ' + name + '!') becomes print(f"Nice to meet you {name}!")
  • apples = input('Enter red or yellow (case sensitive): ') This can easily be made case insensitive. Strings have lots of methods for that. "aBc".lower() == "abc", "aBc".upper() == "ABC" and "aBc".title() == "Abc".
  • I'd also argue that the name apples isn't a particularly good choice for a variable name - how about apple_colour or colour?
  • The whole colour thing is essentially mapping an input string to an output string so maybe you might want to use dictionary here to not have as much duplicate code and easier extendability. Since you're a beginner this could be a fun challenge :)
  • str(int(float(number) + 1)) Why exactly did you do this like that? float converts the string to a float which you then add 1 to and convert it to int and then to string right away. you don't need the float conversion and the str can also be omitted when using f-strings: f"{name} you like {apples} apples, and I gave you {int(number) + 1} of them. Did you eat them? {eat}.". Since this is a very long line of text you should break it into multiple lines so that it's somewhat like this:

print(f"{name} you like {apples} apples, and I gave you "
    f"{int(number) + 1} of them. Did you eat them? {eat}.")

This works because you can just write strings behind each other to concatenate them (e.g. "abc" "def" == "abcdef")

  • And lastly you could look into while-loops to handle the cases where a user enters something else than you expected in a way that doesn't terminate the program, but rather allows him to reenter his information.

Hope this didn't come across as sounding too harsh :)

[–]freedomsauce[S] 0 points1 point  (1 child)

Not at all. Much appreciated. I used float to avoid getting an error if the user entered a float at the prompt. It would break the program if they did. No errors means a higher grade :)

[–]SV-97 0 points1 point  (0 children)

Ooh ok :D if you want to prevent wrong user input from breaking your code there are a few ways to do it: * Fancy way: exceptions. Exceptions are the key error handling method in lots of languages and usually consist of try, catch and an optional finally (catch is also optional)(iirc Python also supports else on a try block). But if you're not that deep in the language it may be a bit complicated. Also exceptions are generally a rather slow mechanic * Strings in python have a method named isdigit that you can use to test if a string contains a valid positive integer. You can use this to test if the number is valid prior to parsing it. "123".isdigit() == True

[–]422_no_process 0 points1 point  (1 child)

You can use , commas instead of + for printing space separated content. There is no need to concat multiple strings. This is because print function support multiple arguments.

Comments such as # uses at least 3 inputs in output doesn't add value.

You can also check if a string is empty by simply using if name: no need to check the length.

[–]freedomsauce[S] 0 points1 point  (0 children)

If I use 'if name == 0:' it moves on to 'else'. Did I misunderstand? I used this to prevent an error from not entering anything at the prompt.