If语句是否按预期工作

2024-04-25 23:54:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序首先询问用户他们的手机出了什么问题,当用户写下答案时,程序检测关键字并输出解决方案。我用了“或”语句,但当我输入“我的手机掉进水里,我的扬声器坏了”时,它应该会输出“你需要新的扬声器。如果waters与您联系过,您可能还需要一个新的主板,如果它没有启动,但它会输出“您需要更换屏幕”

有谁能告诉我我做错了什么,告诉我今后如何克服这个问题。你知道吗

import time #This adds delays between questions
name = input ("what is your name?")
problem1 = input("hello there,what is wrong with your device?")
if "cracked" or "screen broke" in problem1:
    print("you will need your screen replacing")
elif "water" or "speakers" in problem:
    print("you need new speakers. if waters been in contact you also may need a new motherboard if it doesnt come on")

Tags: or用户namein程序youinputyour
1条回答
网友
1楼 · 发布于 2024-04-25 23:54:07

我想你弄错了。在编程中,or与英语略有不同。x or y in z表示"is x True or is y in z?"。如果第一个参数(x)是非空的,这意味着它的计算结果是True。不检查z中是否存在任何东西。如果x为空,则计算结果为False,并检查yz中的存在,但不检查x的存在。您需要为每个测试创建一个新的测试。另外,您在第二个elif中使用了problem而不是problem1

if "cracked" in problem1 or "screen broke" in problem1:
    ...
elif "water" in problem1 or...
    ...

相关问题 更多 >