Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
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 68,404 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!