-1

我正在学习 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
4

2 回答 2

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

这回答了你的问题了吗?

于 2018-12-10T19:20:23.610 回答
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')

我认为你没有完全理解缩进。这不像任何其他语言。您需要正确地进行缩进。

希望对你有帮助

于 2018-12-10T19:22:05.490 回答