Welcome to the Treehouse Community
Looking to learn something new?
I'm a little lost on what try and except do, it seemed like it was working with out it
What's the purpose of try and except
Chris FreemanTreehouse Moderator 67,989 Points
try statement allows you to run code where the outcome is uncertain. The is usually used in cases where you are dealing with results outside of you control (user input, working OS resources) or to verify the code functionality is acceptable. The
except statement defines what to do in case of various errors are actually seen.
In other languages, the onus is on the programmer to protect against bad conditions explicitly, such as checking if a denominator is not zero before dividing. In python, the philosophy is "Easier to ask for forgiveness than permission" EAPF. This is contrasted to the "Look before you leap" LBYL philosophy.
guess = int(input("Guesss a number between 1 and 10"))
doesn't this statement say that only a number can be used...or was the try and except used as a way to get feedback?
[MOD: added ```python formatting -cf]
I see, thank you!
Benito Thompson1,650 Points
Apparently you cannot put "print(...... .format(guess))" underneath the except ValueError like you did in the video, because if you get a ValueError, it means that the guess was not assigned to the variable in the first place so it is impossible to call it. When I put print("That isn't a number!") then it worked.
I'm not sure if what I said made sense. But someone tell me if that is right or if I missed something. Thank you!