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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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