python - 字符串的 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?
解决方案
or 不能这样工作。用以下内容替换该行:
if order not in ("mocha", "frappuchino", "cappuchino"):
推荐阅读
- google-chrome - CKEditor 4:粘贴 div 标签转换为 p 标签(Chrome)
- image - 在 Vue 中使用文件加载器有条件地渲染图像
- c# - 检查字符串是否只包含小写字母和符号
- react-native - 为什么 ESLint 在使用 flow 时会抛出错误?
- java - 列表中的特定组合
- javascript - 如何在 npm 包中添加文件夹作为条目?
- javascript - 在哪里可以找到有关流规范的更多信息
- javascript - 如何在 jQuery 插件中使用 .each()
- python - 如何有效地计算掷骰子的总和?
- c++ - 如何在 Qt 中打开和最小化外部应用程序到系统托盘