0

No mather what input I give I get the output "I think you're to old to go on an adventure. Your adventure ends here." Like if I input 17 I get "I think you're to old to go on an adventure. Your adventure ends here." and if I input 18-59 I get "I think you're to old to go on an adventure. Your adventure ends here."

while True:
    age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
    if age.isdigit() == False:
        print "\n Please, put only in numbers!"
        time.sleep(3)
    elif age < 18:
        print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
        time.sleep(7)
        exit(0) 
    elif age >= 60:
        print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
        time.sleep(5)
        exit(0)
    else:
        print "\n %s. You're starting to get old." % age
        break
1
  • 1
    raw_input gives you a string, every time, so you can't compare it to numbers. Try int(raw_input(...)). Commented Aug 23, 2014 at 0:38

2 Answers 2

3

You need to compare your input as an int

age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))

Otherwise you are comparing a str to an int. See the following example

>>> 5 < 10
True
>>> type(5)
<type 'int'>

>>> '5' < 10
False
>>> type('5')
<type 'str'>

In Python 2.x, comparing values of different types generally ignores the values and compares the types instead. Because str >= int, any string is >= any integer. In Python 3.x you get a TypeError instead of silently doing something confusing and hard to debug.

Sign up to request clarification or add additional context in comments.

6 Comments

One problem: he's checking age.isdigit, so this is too early to convert it to int. You either need to move the conversion into an else clause under that first if, or use a try/except instead of an if, or otherwise restructure things in some way.
That gives me a 'int' object has no attribute 'isdigit' Is it because 'isdigit' only works with strings?
Correct. If you try to cast a str to an int that contains letters, for example, you will get a ValueError. So you could check for digits first before trying to cast.
In that case, try testing on elif int(age) < 18 and elif int(age) >= 60.
That still gives me 'int' object has no attribute 'isdigit'
|
1

The problem is that raw_input always returns a string object, and those don't really compare to int types. You need to do some type conversion.

If you want to use isdigit to test if the input is a number, then you should proceed this way:

while True:
    age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
    if age.isdigit() == False:
        print "\n Please, put only in numbers!"
        time.sleep(3)
    elif int(age) < 18:
        print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
        time.sleep(7)
        exit(0) 
    elif int(age) >= 60:
        print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
        time.sleep(5)
        exit(0)
    else:
        print "\n %s. You're starting to get old." % age
        break

However, you can simplify the code a little by converting to an integer right away, and just catching the exception if it's an invalid string:

while True:
    try:
        age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
        if age < 18:
            print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
            time.sleep(7)
            exit(0) 
        elif age >= 60:
            print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
            time.sleep(5)
            exit(0)
        else:
            print "\n %s. You're starting to get old." % age
            break
    except ValueError:
        print "\n Please, put only in numbers!"
        time.sleep(3)

1 Comment

Thanks, worked perfect with the try and except exceptions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.