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

Python

except ValueError: print("{} is not a valid number".format(guess_num))

Hey guys, when I want to test the exception, like I type a character instead of num, it will show this error. I do not know why

import random def guess(): secret_num = random.randint(1,5) guess=[] while len(guess)<3: try: guess_num=int(input("Please guess num 1 to 5 ")) except ValueError: print("{} is not a valid number".format(guess_num)) else: if guess_num == secret_num: print("Your are right!") break else: print("Your wrong, please try again") guess.append(guess_num)
guess()

Hey Yougang! Code that is not formatted is really hard to read and even hard to debug! Please check the "Markdown Cheatsheet" at the bottom of the page (Just below add an answer) to see how to format code correctly! Add a comment with the updated code and ill be sure to take a look!

import random
def guess():
    secret_num = random.randint(1,5)
    guess=[]
    while len(guess)<3:
        try:
            guess_num=int(input("Please guess num 1 to 5 "))
        except ValueError:
            print("{} is not a valid number".format(guess_num))
        else:
            if guess_num == secret_num:
                print("Your are right!")
                break
            else:
                print("Your wrong, please try again")
            guess.append(guess_num)      
guess()

2 Answers

Hey again Yougang! Thanks for the updated code. I do not get an error while running this program tho, it functions as it should. Im gonna assume what your reffering to tho is that if you type a non integer it will still just output the last integer you inputted. This is a simple fix tho, but first why does your code not work. Well the issue is with your try statement. You see your assigning the variable guess_num to whatever you get when turning the input into an integer. However if it cant be turned into an integer it will go to the except ValueError and do what is said there. So if you input "a" it will not be assigned to guess_num because it will just jump to except ValueError. This is also why when you input a non integer before anything else, the program will crash with the message: local variable 'guess_num' referenced before assignment. This makes sense tho right, because you were trying to format the guess_num variable, but it didnt contain anything. To fix this were gonna have to get around this issue that if its a non integer the program will imidiately go to except ValueError. How about we try to save what the user inputted into the variable first, and THEN check if its an integer. Code:

import random
def guess():
    secret_num = random.randint(1,5)
    guess=[]
    while len(guess)<3:
        try:
            guess_num= input("Please guess num 1 to 5 ") #So whatever the user inputs is stored into this variable regardless of wether its an integer or not
            guess_num = int(guess_num) #Here we try to turn it into an integer and if that cant be done well go to except ValueError. But still what the user inputted will be in the variable guess_num because we already assigned it
        except ValueError:
            print("{} is not a valid number".format(guess_num))
        else:
            if guess_num == secret_num:
                print("Your are right!")
                break
            else:
                print("Your wrong, please try again")
            guess.append(guess_num)      
guess()

Hope this helps!

I follow the video and did not think too deep, now I feel so good, again thank you for your help

Glad to help! You can mark the question as ‘solved’ by sellecting a ‘Best answer’.

Thank you so much for your help