2

I'm going through the book Python Crash Course and ran into a little hiccup on one of the exercises. Basically it asks you to create a while loop that tells the user to input their age and it will return the price of a ticket based on their age. This is supposed to repeat until the user types 'quit'. Pretty simple, except I'm confused as to how I would go from converting the input from an integer (their age) to a string ("quit"). I get the error: "invalid literal for int() with base 10: 'quit'" whenever I try to type quit. This is what I have so far:

age_prompt = "\nWrite in your age: "
age_prompt += "\nType 'quit' to exit "

while True:
    age = int(input(age_prompt))
    if age < 3:
        print("Your ticket is free.")
    elif age < 12:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")

    if age == 'quit':
        break
Matt
  • 21
  • 3

5 Answers5

4

You would need to test if the variable was "quit" before converting to an integer (because "quit" obviously isn't a number, so Python rightly complains).

Here's how you could do it:

while True:
    age = input(age_prompt)
    if age == 'quit':
        break

    try:
        age = int(age)
        if age < 3:
            print("Your ticket is free.")
        elif age < 12:
            print("Your ticket is $10")
        else:
            print("Your ticket is $15")
    except ValueError:
        print("Invalid input. Please enter a valid number or 'quit'")
Aurora0001
  • 13,139
  • 5
  • 50
  • 53
1
age_prompt = "\nWrite in your age: "
age_prompt += "\nType 'quit' to exit "

while True:
    try:
        age = input(age_prompt)
        age = int(age)
        if age < 3:
            print("Your ticket is free.")
        elif age < 12:
            print("Your ticket is $10")
        else:
            print("Your ticket is $15")

    except ValueError:
        if age == 'quit':
            break

Check to see if it is an int. If not, check if it is 'quit'

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • Any particular reason you used exceptions here? OP is new to python, so this may not be the best thing to recommend. – kirkpatt Jul 28 '16 at 15:21
  • @kirkpatt Being new to Python is the more reason one should learn about exception hadling – Moses Koledoye Jul 28 '16 at 15:22
  • Exceptions are pretty basic to writing python code. They aren't complicated, and they are necessary – joel goldstick Jul 28 '16 at 15:23
  • All I mean is that based on the level that OP is at, it may be best not to try introducing new concepts while they are solving other problems. The question mentions Python Crash Course, which has a section on exceptions, so they'll get there. – kirkpatt Jul 28 '16 at 15:26
  • Awesome thank you! Yeah I just started learning a couple of days ago so all of this is new to me. – Matt Jul 28 '16 at 15:29
  • If you like it best, check the check box to accept. YW – joel goldstick Jul 28 '16 at 15:57
0

User input is received as a string from the call to input(). In your example, you are directly converting the output of input() into an integer:

age = int(input(age_prompt))

Once you have converted the input to an integer, you can no longer compare the integer to the string "quit" as they are not directly comparable. You can process the input string before converting to an integer.

# read user input as a string
user_input = input(age_prompt)

if user_input == "quit":
    quit()
elif user_input == SOME_OTHER_COMMAND:
    other_command()
else:
    # try to convert input into an integer
    try:
        age = int(user_input)
    except ValueError:
        print "Input '%s' is invalid!"
        quit()
    if age < 3:
        ...
theorifice
  • 670
  • 3
  • 9
0

Try this:

age_prompt = "\nWrite in your age: "
age_prompt += "\nType 'quit' to exit "

while True:
        age = raw_input(age_prompt)

        if age == 'quit':
            break
        else:
            age = int(age)
            if age < 3:
                    print("Your ticket is free.")
            elif age < 12:
                    print("Your ticket is $10")
            else:
                    print("Your ticket is $15")
echo
  • 84
  • 1
  • 8
-1

you would have to use seperate variables (or exceptions - without example)

while True:
    ui = input(age_prompt)
    if ui == 'quit':
        break

    age = int(ui)
    if age < 3:
        print("Your ticket is free.")
    elif age < 12:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")
janbrohl
  • 2,626
  • 1
  • 17
  • 15