简体   繁体   English

Python 或字符串操作

[英]Python Or Operation for strings

I am working on an entry-level project of an automated coffee shop and I ran into a small problem with my code.我正在做一个自动化咖啡店的入门级项目,我的代码遇到了一个小问题。 I have this code if order != "mocha" or "frappuccino" or "cappuccino": in order to filter out selections at the very first choice.我有这个代码if order != "mocha" or "frappuccino" or "cappuccino":为了过滤掉第一个选择的选择。 But when I try to run my code, the response from the function (yes_or_no_question) also seem to go through the if statement mentioned above and I can not figure out why.但是当我尝试运行我的代码时,函数(yes_or_no_question)的响应似乎也通过了上面提到的 if 语句,我不知道为什么。 I do suspect that it is a problem with the OR operation and strings if not a logical error with the way I have my code set up.如果不是我的代码设置方式的逻辑错误,我确实怀疑这是 OR 操作和字符串的问题。 I had included my test run errors below, thank you all for your valuable time to help out beginners.我在下面包含了我的测试运行错误,感谢大家抽出宝贵的时间来帮助初学者。

#side functions
def ask_amount_m():
    amount = int(input("How many would you like?"))
    response_1 = "{} mocha coming up".format(amount)
    print(response_1)
    return amount
def ask_amount_f():
    amount = int(input("How many would you like?"))
    response_2 = "{} frappuccino coming up".format(amount)
    print(response_2)
    return amount
def ask_amount_c():
    amount = int(input("How many would you like?"))
    response_3 = "{} cappuccino coming up".format(amount)
    print(response_3)
    return amount
**def yes_or_no_question():**
    x = input("Would you like to order anything else?")
    if x == "yes":
        first_step()
    if x == "no":
        response_bye = "Have a nice day!"
        print(response_bye)

#main code starts here
def first_step():
    order = input("What would you like to order today?")
    if order == "mocha":
        ask_amount_m()
        yes_or_no_question()

    if order == "frappuccino":
        ask_amount_f()
        yes_or_no_question()

    if order == "cappuccino":
        ask_amount_c()
        yes_or_no_question()

    **if order != "mocha" or "frappuccino" or "cappuccino":**
        print("Try again")
        first_step()

first_step()
Test run error example:
What would you like to order today?kk
Try again
What would you like to order today?kjgg;]
Try again
What would you like to order today?frappuccino
How many would you like?234
234 frappuccino coming up
Would you like to order anything else?no
Have a nice day!
Try again
What would you like to order today?

The or doesn't work like this.或 不能像这样工作。 Replace the line with the following:用以下内容替换该行:

if order not in ("mocha", "frappuchino", "cappuchino"):

试试这个: if order != "mocha" or order != "frappuccino" or order != "cappuccino":**

You could use the in operator:您可以使用in运算符:

if order not in ["mocha", "frappuccino", "cappuccino"]:
    # better grab some tea

Try not to use recursion in python.尽量不要在python中使用递归。 Consider something like like this code.考虑类似这样的代码。

def ask_amount(kind_of_coffee):
    amount = int(input("How many would you like?"))
    print("{} {} coming up".format(amount, kind_of_coffee))
    return amount

def is_finished():
    x = input("Would you like to order anything else?")
    if x == "no":
        response_bye = "Have a nice day!"
        print(response_bye)
        return True
    return False

ACCEPTED_BEVRAGES = ["mocha", "frappuccino", "cappuccino"]

#main code starts here
def first_step():
    finished = False

    while not finished:
        order = input("What would you like to order today?")
        if order in ACCEPTED_BEVRAGES:
            # The order is for a "mocha", a "frappuccino", or a "cappuccino"
            ask_amount(order)

            finished = is_finished()
        else:
            print("Try again")


first_step()

How ever, you had a big problem in your code that is worth pointing out但是,您的代码中有一个值得指出的大问题

if order != "mocha" or "frappuccino" or "cappuccino" will not work in python. if order != "mocha" or "frappuccino" or "cappuccino"在 python 中不起作用。 You need to be specific and write it like if order != "mocha" or order != "frappuccino" or order != "cappuccino" .您需要具体并写成if order != "mocha" or order != "frappuccino" or order != "cappuccino" Even better would be to check for absence in list, see updated code for this.更好的是检查列表中的缺失,请参阅更新的代码。

Also, you really don't have to use so many different functions that does exactly the same thing.此外,您真的不必使用这么多不同的功能来完成相同的事情。 Fuctions can accept parameters too.函数也可以接受参数。

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

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