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! While you're at it, check out some resources Treehouse students have shared here.
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
Philip Schultz
11,437 PointsTry Block Error
Can some tell me what I'm doing wrong here. Whene I put in a non-int the Value Error will not display. it says 'unbound local error'. Thanks for the help!
def game():
try:
selected_by_user = int(input("Please select a number between 1 and 10."))
except ValueError:
print("Sorry, but {} is an invalid number".format(selected_by_user))
else:
print(selected_by_user)
game()
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! This is a very common one for people to get tripped up on so you're not alone. But let's think for a minute about some basic rules about Python and what's happening here. First, in order to use a variable you must first assign it a value. In your case, you are declaring and initializing the variable selected_by_user in the try block. Then you take that and turn it into an integer (hopefully), but here's where the problem comes in. If the user enters something like "hello", then the conversion to an integer will fail. When this fails, the assignment to selected_by_user also fails. At this point, it jumps to the ValueError without selected_by_user being defined.
In your except clause you then try and print out an error to the user saying that you need a number and show the user what they put in. But you're trying to format the string with a value from a variable that isn't defined. Remember, that assignment failed.
The most elegant way to fix this (in my opinion) is to move the prompt to the user out of the try block. Then, inside the try block just do the conversion to integer.
Take a look:
def game():
#get the input from the user no matter what it is
selected_by_user = input("Please select a number between 1 and 10.")
try:
#try storing the result of the input converted to an integer
selected_by_user = int(selected_by_user)
except ValueError:
print("Sorry, but {} is an invalid number".format(selected_by_user))
else:
print(selected_by_user)
game()
This will guarantee that your selected_by_user variable is defined and has a value to use to format that string even if the conversion to an integer fails.
Hope this helps!
Philip Schultz
11,437 PointsOops, didn't catch that. Thanks again Jennifer.
Jennifer Nordell
Treehouse TeacherOn a side note: good on you for experimenting! Not everyone catches this. Many just move right along
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsThank you for the help, Jennifer! Just to be clear, can you watch 1:48 seconds into the video 'Number Game Refinement' (Python Basics). I just want to make sure that the instructor made a mistake in the video or if he is doing something different that I didn't catch. How did he get it to work? I appreciate your help!
link: https://teamtreehouse.com/library/python-basics/number-game-app/number-game-refinement
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherPhilip Schultz No, you're not missing anything in the video and to be clear, he doesn't show this working. It's not a mistake rather a bit of an "Easter Egg". Take a look at the Teacher's Notes for this video. This bug is absolutely intentional.