简体   繁体   English

不知道为什么它拒绝我的 else 声明

[英]Don't know why it's rejecting my else statement

drinks = 5
if drinks <= 2:
  print("Keep going buddy")
elif drinks == 3:
  print("I believe you've had enough, sir")
  else:
    print("stop")

This was the code I was trying to run, and it keeps giving me a syntax error with the "else" and I don't know why.这是我试图运行的代码,它一直给我一个带有“else”的语法错误,我不知道为什么。

Your syntax is wrong.你的语法是错误的。 Remove the space before the else:删除else:

The syntax of your code is labeled as incorrect because an else statement and clause cannot exist without an if statement and clause preceding it.您的代码的语法被标记为不正确,因为else语句和子句如果没有前面的if语句和子句就无法存在。 Since you have an else statement alone inside an elif clause, an error is thrown.由于在elif子句中有单独的else语句,因此会引发错误。

Likely this is just an issue with your indentation, which you need to be very careful about, since Python emphasizes whitespace.可能这只是您的缩进问题,您需要非常小心,因为 Python 强调空格。 Here is the code I believe you meant to type:这是我相信您要输入的代码:

drinks = 5
if drinks <= 2:
    print("Keep going buddy")
elif drinks == 3:
    print("I believe you've had enough, sir")
else:
    print("stop")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM