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 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?