如果声明有或没有按预期工作 [英] If statement with or not working as expected
问题描述
我的程序首先询问用户手机有什么问题,当用户写回答时,程序会检测关键字并输出解决方案。我使用了或语句但是当我输入我的手机掉在水中而我的扬声器无法工作时,它应该输出你需要新的扬声器。如果有水接触你也可能需要一个新的主板,如果它没有出现但它输出你将需要你的屏幕替换
My program first asks the user what is wrong with their mobile phone and when the user writes their answer, the program detects keywords and outputs a solution. I used "or" statements but when I input "My phone dropped in water and my speakers don't work" it should output "you need new speakers. if waters been in contact you also may need a new motherboard if it doesn't come on" but instead it outputs "you will need your screen replacing"
任何人都可以告诉我我做错了什么,告诉我将来如何克服这个问题。
Can anybody tell me what I'm doing wrong and tell me how I can overcome this problem in the future.
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")
推荐答案
我认为您误解或
。在编程中,或
与英语略有不同。 z
中的x或y表示是x是真还是z中的y?
。如果第一个参数( x
)非空,则表示它的计算结果为 True
。没有检查 z
中存在的任何内容。如果 x
为空,则计算结果为 False
并且检查 y
存在 z
,但不是 x
的存在。您需要为每个测试创建一个新测试。此外,您在第二个 elif
问题而不是 problem1
>:
I think you mis-understand or
. In programming, or
is slightly different from in English. x or y in z
means "is x True or is y in z?"
. If the first argument (x
) is non-empty, that means that it evaluates to True
. No check is made for anything's presence in z
. If x
is empty, it evaluates to False
and there is a check for y
's presence in z
, but not x
's presence. You need to create a new test for each one. Also, you used problem
instead of problem1
in your second elif
:
if "cracked" in problem1 or "screen broke" in problem1:
...
elif "water" in problem1 or...
...
这篇关于如果声明有或没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!