简体   繁体   English

初学者Python-在循环中使用字符串或变量进行回答

[英]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. 我知道age = int(age)将用户输入定义为整数。 My question is how can I change it to to not treat 'quit' as an integer and end the loop when 'quit' is input. 我的问题是如何将其更改为不将“ quit”视为整数,并在输入“ quit”时结束循环。

If age is 'quit' , you will break anyway. 如果age'quit' ,您还是会破产。 Therefore, you can just use if for the next one instead. 因此,您可以将if用作下一个。 As long as you do that anyway, you can make it an int after that if : 只要您这样做,之后就可以将其设置为int, 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. 实际上,我会在这里介绍一个try/except

The main goal of your application is to gather ages. 您的应用程序的主要目标是收集年龄。 So, wrap your input with a try/except to always get an integer. 因此,用try / except包裹您的输入,以始终获取整数。 If you get a ValueError , you fall in to your exception block and check to see if you entered quit . 如果收到ValueError ,则进入异常块并检查是否输入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. 但是,如果用户未输入quit ,而是输入其他字符串,则会提示您该输入无效,并且它将继续询问用户有效年龄。

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. 同样,为了确保您不会错过任何可能用不同大小写键入的“退出”消息,您可以始终将输入设置为lower以始终比较字符串中的相同大小写。 In other words, do age.lower when you are checking for the entry to be quit . 换句话说,当您检查要quit的条目时,请使用age.lower

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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