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?