[英]else if and elif not working for my python code
这是我学习编码的第一天。 我试过这段代码,不知道为什么会失败。 任何帮助深表感谢:
a=int(input())
if a>10:
print("your number is greater than 10")
else:
我得到SyntaxError: invalid syntax
if a>10:
print("your number is greater than 10") #indent the print statement
else:
print('Not greater than 10')
#you need to perform an action inside the else condition
您的缩进不正确。 它应该如下所示:
a=input()
if a>10:
print("your number is greater than 10")
else:
# You didn't have anything in your else statement so either remove it or add a statement...
print("your number is <= 10")
请参阅有关缩进的 python 文档。
你有一个缩进错误:
if condition:
code...
elif condition:
another code...
else:
last code...
其中elif
和else
部分是可选的, elif
表示else: if condition:
。
PS:使用int(input())
否则你会得到一个字符串但没有一个 int。
使用 control-j 和 control-left-bracket 进行缩进,使用 control-right-bracket 进行 dedent 将使您重新控制缩进。 当您在解释器中键入行时,它会错误地管理缩进。 由您决定是否正确。
因此,您正在关注的非正式教程需要更新。 它可能对旧版本的 Python “有效”。 让它适用于所有人将是一项任务,因为这些年来语言已经发生了变化。
兄弟,缩进在Python中非常重要。 由于 else: 是另一个条件表达式,它应该独立于任何其他条件表达式。 因此,它必须放在 if a>10: 的缩进之外,并且里面应该有一个语句,它将在满足它所属的上述条件时执行。 因此,正确的代码是:-
a=int(input())
if a>10:
print("your number is greater than 10")
else:
print("your number is not greater than 10")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.