这是我学习编码的第一天。我尝试了这段代码,但不知道为什么失败了。任何帮助都非常感谢:
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())
否则你会得到一个字符串,但没有一个整数。
使用 control-j 和 control-左括号进行缩进,使用 control-right-bracket 进行缩进,将使您重新控制缩进。 当您在解释器中键入行时,它会错误地管理缩进。 由你来纠正它。
因此,您遵循的非正式教程需要更新。它可能已经"工作"于旧版本的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")