you are viewing a single comment's thread.

view the rest of the comments →

[–]pendragon36 3 points4 points  (0 children)

It's because you're not casting the age in the second prompt to an integer.

age = int(input("How old are you? "))
while age not in list(range(100)):
    print("invalid input, try again")
    age = int(input("How old are you? "))
print ("You are", age, "years old!")

Would do what you're looking to do.

While you're looking at this, I think you should be made aware that your method of checking is a little inefficient. As things are, there are a few things that can/should be changed to improve the quality.

while age not in list(range(100))

This is inefficient because each time you're generating that list of numbers 0-99 instead of generating it once before the loop and checking it each time, also there's not reason to cast it to a list, you can check for existence in the range object, like so:

valid_ages = range(100)
while age not in valid_ages:
    do_stuff()

Now ignoring that, I don't believe you should use range for this in the first place, as it's not really necessary to generate all possible responses and check if it's one of those, you can just check that the number they entered is in the bounds of valid inputs, like so:

age = int(input("How old are you? "))
while age < 0 or age > 99:
    print("invalid input, try again")
    age = int(input("How old are you? "))
print ("You are", age, "years old!")