I'm trying to get my little user-input program to recognize that words aren't numbers.

Without any logic parameters the program just crashes when you put in anything but a number, I'm attempting to allow the user to try again and put in a number using a while loop, but I keep getting syntax errors when I try to define valid_age as any number between 1 and 200.

The coding for the defining of valid_age and the while loop follows.

age = 0
[B]def valid_age (range(1, 200))[/B]
age = int(input('How old are you today? '))
while age != valid_age:
    print()
    print('That is not a valid age!')
    age = int(input('How old are you today? '))
else:
    age10 = int(age) + 10
    print()
    print('In 10 years you will be ' + str(age10) + ' years old.')
print()

Recommended Answers

All 8 Replies

Just a quick question, is this Python 3.0 or 2.6?

EDIT: nevermind, that was the stupidest question...

Not tested but should be close. In these types of programs, you want to set a variable to assume that there are errors and unset it only when all conditions are met.

errors = True
while errors:
   age = input('How old are you today? ')
   try:
      age_int = int(age)
      if (age_int > 0) and (age_int < 200):
         errors = False
         print('\nIn 10 years you will be %d years old.' % (age_int+10))
      else:
         print('\nThat is not a valid age! \n')
   except:
      print('\nThat is not a valid age! \n')
while age not in valid_age:

Also, you really should use raw_input I suppose I could throw out a different solution:

valid_age = range(1, 200)
try:
    age = int(raw_input('How old are you today? '))
except ValueError:
    age = 0
while age not in valid_age:
    print('That is not a valid age!')
    try:
        age = int(raw_input('How old are you today? '))
    except ValueError:
        age = 0

Or even better:

def age_input():
    try:
        return int(raw_input('How old are you today? '))
    except ValueError:
        return 0

valid_age = range(1, 200)
age = age_input()
while age not in valid_age:
    print('That is not a valid age!')
    age = age_input()

Here's my contribution:

>>> def age_input(lower=1, upper=200):
... 	while True:
... 		age = raw_input("Enter an age between %d and %d" % (lower,upper))
... 		if age.isdigit():
... 			age = int(age)
... 			if lower <= age <= upper:
... 				return age
... 		print "Invalid answer. Try again."

Based on the statement
age = int(input('How old are you today? '))
and the print() statements, we are assuming that the OP is probably using 3.0, which doesn't have a raw_input. But someone should have asked before now. We'll have to get used to asking for the Python version number.

Thanks again for all of your help! I managed to get the program working and now it recognizes that words aren't valid ages. :icon_razz:

You should post it for others that might come across this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.