简体   繁体   中英

Beginner Python - Answering with either a string or a variable in a loop

My code so far:

prompt = "\nEnter 'quit' when you are finished."
prompt += "\nPlease enter your age: "

while True:
    age = input(prompt)
    age = int(age)

    if age == 'quit':
        break
    elif age <= 3:
        print("Your ticket is free")
    elif age <= 10:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")

The program runs fine unless you enter 'quit' to end the loop. I understand that age = int(age) defines the user input as an integer. My question is how can I change it to to not treat 'quit' as an integer and end the loop when 'quit' is input.

If age is 'quit' , you will break anyway. Therefore, you can just use if for the next one instead. As long as you do that anyway, you can make it an int after that if :

while True:
    age = input(prompt)

    if age == 'quit':
        break
    age = int(age)

    if age <= 3:
        print("Your ticket is free")
    elif age <= 10:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")

You should probably take care of those cases when the user types something else, however, so I would suggest the following:

while True:
    age = input(prompt)

    if age == 'quit':
        break
    elif not age.isdigit():
        print("invalid input")
        continue

    age = int(age)

    if age <= 3:
        print("Your ticket is free")
    elif age <= 10:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")

I would introduce a try/except here, actually.

The main goal of your application is to gather ages. So, wrap your input with a try/except to always get an integer. If you get a ValueError , you fall in to your exception block and check to see if you entered quit .

The application will tell the user it is quitting and break out. However, if the user did not enter quit , but some other string, you are told that the entry is invalid, and it will continue to ask the user for a valid age.

Also, just to make sure you never miss a 'quit' message that could be typed with different cases, you can always set the input to lower to always compare the same casing in your string. In other words, do age.lower when you are checking for the entry to be quit .

Here is a working demo:

prompt = "\nEnter 'quit' when you are finished."
prompt += "\nPlease enter your age: "

while True:
    age = input(prompt)
    try:
        age = int(age)
    except ValueError:
        if age.lower() == 'quit':
            print("Quitting your application")
            break
        else:
            print("You made an invalid entry")
            continue

    if age <= 3:
        print("Your ticket is free")
    elif age <= 10:
        print("Your ticket is $10")
    else:
        print("Your ticket is $15")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM