我正在学习python,并且正在尝试一组练习,但是我被困在下一个练习中:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A") elif score >= 0.8:
print ("B") elif score >= 0.7:
print ("C") elif score >= 0.6:
print ("D") elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
但是我收到下一条消息错误:
IndentationError: unindent does not match any outer indentation level on line 7 elif score >= 0.8: ^ in main.py
答案 0 :(得分:0)
缩进(=每行前面的制表符/空格数)在python中很重要。您发布的代码缩进不正确。正确的缩进应如下所示:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
第一行始终不缩进。当开始一个块时(例如try:
,if:
,elif:
,...),属于该块的所有后续行都比开始行缩进4个空格。通过写缩进较少的下一条语句来“关闭”一个块。
另一个例子:
if False:
print(1)
print(2)
# prints nothing because both print statements are part of the if condition
if False:
print(1)
print(2)
# prints 2 because the `print(2)` statement is not part of the if condition
这能回答您的问题吗?
答案 1 :(得分:0)
您的缩进应该是这样的:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
我认为您不太了解缩进。这不同于其他任何语言。您需要正确进行缩进。
希望对您有帮助