简体   繁体   English

为什么这个 elif 语句在第 5 行出现错误?

[英]Why am I getting an error in line 5 for this elif statement?

I have to write a simple program to give assign a letter grade to a test score from 0.0 - 1.0我必须编写一个简单的程序来为 0.0 - 1.0 的测试分数分配一个字母等级

score = input("Enter Score: ")
g = float(score)

if 0.0 <= g <= 1.0:
    elif 0.9 <= g <= 1.0:
        print("A")
    
    elif 0.8 <= g < 0.9:
        print("B")
    
    elif 0.7 <= g < 0.8:
        print("C")
    
    elif 0.6 <= g < 0.7:
        print("D")
    
    elif 0.0 <= g < 0.6:
        print("F")  
else:
    print("Error, given value is out of the 0.0-1.0 range")
    quit()

The if-elif statement on a new block should start with if.新块上的 if-elif 语句应以 if 开头。 It can't start with elif.它不能以 elif 开头。

Try it this way:试试这种方式:

score = input("Enter Score: ")
g = float(score)

if 0.0 <= g <= 1.0:
    if 0.9 <= g <= 1.0:
        print("A")
    
    elif 0.8 <= g < 0.9:
        print("B")
    
    elif 0.7 <= g < 0.8:
        print("C")
    
    elif 0.6 <= g < 0.7:
        print("D")
    
    elif 0.0 <= g < 0.6:
        print("F")  
else:
    print("Error, given value is out of the 0.0-1.0 range")
    quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM