The biggest problem is that you are not saving the conversion to int , so you use the assumption as a string entered by the user. You need to save it by doing user = int(raw_input('>>>'))
There are other ways to improve this code. You repeat yourself a bit and you do not need random.choice , you can use random.randrange(1, 10)
You cannot just say except: You only want to catch the exceptions you are looking for. The special exception you are looking for is ValueError
In addition, I suggest that you try again when they enter something, not a number. You can complete all this in your own function.
import random def get_user_num(msg='>>> '): """Print the msg parameter as a prompt for the user to enter a number. If they enter an invalid string, reprompt them until they enter a number. """ while True: try: return int(raw_input(msg))
source share