I'm using Python 2.6.6
I have this code:
height = 20
width = 10
x = input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
while x < (width + 1) or x > 20:
print 'That option is not valid'
x = input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
If the user only inputs number, everything works OK but if the user makes a mistake and type, for example q, it gives :
NameError: name 'q' is not defined
I want, if the user inserts a string, the while loop kicks in and give the user: That option is not valid.... How can I solve this without using raw_input since width and height I want to treat tham as numbers?
Regards,
Favolas
EDIT Following Daniel suggestion, I've modified my code to this:
height = 20
width = 10
x = raw_input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
x = int(x)
while x < (width + 1) or x > 20:
print 'That option is not valid'
x = raw_input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
x = int(x)
If the user only inputs int code works as planned but its not safe from user errors. If the users, makes a mistake and types 'q' it gives this error:
ValueError: invalid literal for int() with base 10: 'q'
I understand why but how do I solve this?
Regards,
Favolas