[英]else statement not working: else appears when not supposed to. (Python)
My code is here:我的代码在这里:
import time
idk = input("Hey whats ur name dude ")
print(idk)
time.sleep(1.2)
print("Hmmmm")
time.sleep(1.4)
print("thats a cool name dude")
time.sleep(2)
print("hey", idk)
time.sleep(1.1)
print("uh")
time.sleep(1.4)
dot = input("you know, the last time I actually coded was like a year ago :/. ya think i can still code? (yes/no/maybe) ")
if dot == "yes":
time.sleep(0.7)
print("thx. ill try my best")
if dot == "no":
time.sleep(0.7)
print("ok. i will reteach myself i guess")
if dot == "maybe":
time.sleep(0.7)
print("we will see i guess :]")
else:
print("RESPOND WITH WHAT I TELL U TO RESPOND WITH U IDIOT >:-( ")
In the final part, there is an else statement.在最后一部分,有一个 else 语句。 whenever I run this and I choose the "yes" or "no" option the else statement appears even though it is not supposed to.
每当我运行它并选择“是”或“否”选项时,即使不应该出现 else 语句也会出现。 By the way, this doesn't happen in the "maybe" option.
顺便说一句,这不会发生在“可能”选项中。 I don't think that this is an indentation error.
我不认为这是缩进错误。 I looked through it.
我仔细看了看。 I am really confused.
我真的很困惑。
You should use elif
instead of if
.您应该使用
elif
而不是if
。
So when you enter yes it goes to the next if
and sees it's not no then it goes to the next if
and it's not maybe so it prints the else
message.因此,当您输入yes 时,它会转到下一个
if
并且看到它不是no然后它会转到下一个if
并且它可能不是,因此它会打印else
消息。
You should use elif
in this case, since the else
only applies to your last check at the moment:在这种情况下,您应该使用
elif
,因为else
仅适用于您目前的最后一次检查:
if dot == "yes":
time.sleep(0.7)
print("thx. ill try my best")
elif dot == "no":
time.sleep(0.7)
print("ok. i will reteach myself i guess")
elif dot == "maybe":
time.sleep(0.7)
print("we will see i guess :]")
else:
print("RESPOND WITH WHAT I TELL U TO RESPOND WITH U IDIOT >:-( ")
What is happening here is that the else statement is only affecting the if "maybe".这里发生的事情是 else 语句只影响 if "maybe"。 What you need to do here is to only write one if statement and then write elif statements for the rest:
这里你需要做的是只写一个 if 语句,然后为其余的写 elif 语句:
if dot == "yes":
...
elif dot == "no":
....
elif dot == "maybe":
...
else:
....
Otherwise, if you input "yes", the code will check first if there are any "yes" and print the yes response, but, when it gets to the "maybe" it checks that the statement is false an therefore runs what is inside the else statement.否则,如果您输入“yes”,代码将首先检查是否有任何“yes”并打印 yes 响应,但是,当它到达“maybe”时,它会检查该语句是否为假,因此运行里面的内容else 语句。
Sorry if this is formatted wrong, I'm writing on a phone.抱歉,如果格式有误,我是在手机上写的。
Your elif
only pairs with your final if
statement, which is why the final else statement executes when you select the yes or no options.您的
elif
仅与您的最终if
语句配对,这就是为什么在您选择 yes 或 no 选项时执行最终 else 语句的原因。 You can use elif
for the no and maybe options to make all conditionals part of the same control flow.您可以将
elif
用于 no 和 may 选项,以使所有条件都成为同一控制流的一部分。
https://docs.python.org/3/tutorial/controlflow.html https://docs.python.org/3/tutorial/controlflow.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.