简体   繁体   中英

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

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. It can't start with 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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