[英]Why isn't my python code seeing the condition?
My python code isn't working. 我的python代码无法正常工作。 I have tried many ways of doing it but I can't seem to make this work.
我已经尝试了许多方法来做,但是我似乎无法完成这项工作。 Could anyone tell me what's wrong?
谁能告诉我怎么了?
if not dead:
time.sleep(2)
print('')
print('"Ok next question." She looked at a paper in front of her.')
time.sleep(2)
print('"Where on earth do they a day with 24 hours and a night with 24 hours?"')
time.sleep(2)
if ally:
print('')
print('"Small hint, it starts with A and is a cold desert."')
print('')
answer = input('Which place?:')
else:
answer = input('Which place?:')
if answer == 'Antarctica' or answer == 'antarctica':
time.sleep(2)
print('')
print('She nods. "Correct. Two more.')
if answer != 'Antarctica' and answer != 'antarctica' and ally:
time.sleep(2)
print('')
print('Shaking her head with wide eyes, she looks around carefully.')
time.sleep(2)
print('"The answer is Antarctica." She says, mimicking your voice.')
dead = False
time.sleep(2)
print(''"I can't do that much, so please try to refrain from getting it wrong."'')
if answer != 'Antarctica' and answer != 'antarctica' and not ally:
time.sleep(2)
print('')
print('She sighs. '"I'm sorry."' The fire below comes closer and closer as you fall.')
dead = True
The code just seems to end if I put in a wrong answer, whereas I would like it to see that if the user has an ally, it will give out a different answer, if they don't have an ally they give a different answer etc. Any help would be highly appreciated! 如果我输入错误的答案,代码似乎就结束了,而我希望它能看到,如果用户有一个盟友,它将给出一个不同的答案;如果他们没有一个盟友,他们将给出一个不同的答案。等。任何帮助将不胜感激!
A string can have only one value. 字符串只能有一个值。 What the user entered is either not 'Antarctica' or not 'antarctica'... it can never be both.
用户输入的不是“南极洲”还是“南极洲”…… 永远不能同时存在。
Since and
has higher operator precedence than or
, your current statements are essentially this, further causing unexpected results. 由于
and
具有比or
更高的运算符优先级 ,您当前的语句本质上就是这个,进一步导致意外的结果。
if (answer != 'Antarctica') or (answer != 'antarctica' and ally):
if (answer != 'Antarctica') or (answer != 'antarctica' and not ally):
Use and
instead of or
: 使用
and
代替or
:
if answer != 'Antarctica' and answer != 'antarctica' and ally:
if answer != 'Antarctica' and answer != 'antarctica' and not ally:
You could also take advantage of elif
and else
- there is no need to check the other conditions if one was already true
. 你也可以利用
elif
和else
-有没有需要检查的其他条件,如果一个已经true
。
change 更改
answer != 'Antarctica' or answer != 'antarctica'
to 至
answer.lower() != 'antarctica'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.