[英]Syntax error on 'else' and can't figure out the issue
I started Python yesterday and practiced making small scripts but none of them end up working. 我昨天开始使用Python,并尝试制作小脚本,但最终没有一个工作。 I somewhat get the basics but I just feel so stupid and frustrated. 我有点基础知识,但我只是感到如此愚蠢和沮丧。 Here's one of the scripts. 这是脚本之一。
UserInput = input("Enter")
if UserInput == "yes":
print ("good job")
elif print ("wrong"):
else:
return
There are 3 problems with the code. 代码有3个问题。 The first one is proper indentation. 第一个是适当的缩进。 If your code doesn't look like below, use [tab]* to fix it. 如果您的代码如下所示,请使用[tab] *对其进行修复。 For the other problems see comments in the code: 对于其他问题,请参见代码中的注释:
UserInput = input("Enter")
if UserInput == "yes":
print ("good job")
# elif requires a condition to evaluate. It's a shorthand for else if
# you can do e.g. elif UserInput == "no"
elif (condition): # colon goes here
print ("wrong")
else:
return
Programming requires precise syntax. 编程需要精确的语法。 When you start programming, identifying these small nuisances is difficult. 当您开始编程时,很难识别出这些小麻烦。 Keep trying. 继续尝试。 It will become automatic. 它将变为自动。
*actually you should use 4 spaces, see comment below by Matthias. *实际上您应该使用4个空格,请参阅下面Matthias的评论。 I personally have my IDE configured to insert 4 spaces when I press [tab]. 我个人已将IDE配置为在按[tab]时插入4个空格。
this should help. 这应该有所帮助。 I didn't exactly get what you wanted to do with the elif part , so in this code if and only if the input is yes then it will execute the if condition , otherwise would always execute the else condition. 我并没有完全了解您想使用elif部分做什么,因此在此代码中,当且仅当输入为yes时,它将执行if条件,否则将始终执行else条件。
PS :- It would be good if you can clarify what exactly you want . 附言:-如果您能弄清楚自己到底想要什么,那将是很好的。
UserInput = raw_input("Enter :- ")
if UserInput == "yes":
print ("good job")
else:
print ("wrong")
elif
must specify a condition and indentation must be consistent throughout the module: elif
必须指定条件,并且缩进在整个模块中必须保持一致:
UserInput = input("Enter")
if UserInput == "yes":
print ("good job") # <-+
elif True or False: # | elif needs a condition as well
print ("wrong"): # <-+-- indentation must be indentical
else: # |
return # <-+
Check your indentation 检查您的缩进
UserInput = input("Enter")
if UserInput == "yes":
print ("good job")
else:
print ("wrong")
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.