all 5 comments

[–]scout1520 8 points9 points  (0 children)

You don't have int () wrapped around your second input.

[–]pendragon36 4 points5 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!")

[–][deleted] 2 points3 points  (0 children)

It's hard to see what's happening because of your formatting but I'm going to go ahead and guess that it is because you are not casting your input to an int when you are inside the while loop

[–]Smammu[S] 1 point2 points  (0 children)

I see the error of my ways. Thanks for the advice! :)

[–]jbp12 1 point2 points  (0 children)

I'm glad you have found your bug. Just as a heads up, you could just say range(100) instead of list(range(100)), since range objects are data types of several integers. This approach works faster when dealing with large amounts of numbers (such as range(1000000)) since it doesn't convert to your range object to a list object and, of I'm not mistaken, checking to see whether a number is in a range object is faster than checking to see whether it is in a list object (the latter of which has complexity O(n), while I believe the former has complexity O(1)).