首页 > 解决方案 > 字符串的 Python 或操作

问题描述

我正在做一个自动化咖啡店的入门级项目,我的代码遇到了一个小问题。我有这个代码 if order != "mocha" or "frappuccino" or "cappuccino":为了过滤掉第一选择的选择。但是当我尝试运行我的代码时,函数(yes_or_no_question)的响应似乎也通过了上面提到的 if 语句,我不知道为什么。如果我设置代码的方式不是逻辑错误,我确实怀疑这是 OR 操作和字符串的问题。我在下面包含了我的测试运行错误,感谢大家宝贵的时间来帮助初学者。

#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?

标签: python

解决方案


or 不能这样工作。用以下内容替换该行:

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

推荐阅读