Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Number Game Refinement

Maor Beeri
Maor Beeri
2,154 Points

Need help with input issue

when the user enters something other than a number (for example the char 'g') as an input the program does not know how to handle this case properly. despite this code line that supposed to handle such a case:

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

output example:

Guess a number between 1 and 10: 5
my number is greater than 5
Guess a number between 1 and 10: y
5 is not a number

2 Answers

Majid Bilal
Majid Bilal
3,558 Points

You can handle it by writing it this way:

number = input("Enter a Number: ")
try:
    int(number)
except ValueError:
    print('{} is not a number'.format(number))
Majid Bilal
Majid Bilal
3,558 Points

I think you're converting the input to an integer and y never gets converted to an integer and never gets assigned to "guess" that's why it's not handling it as you'd like.

Maor Beeri
Maor Beeri
2,154 Points

I'm sure this is the issue obviously but how do I fix this bug?