Python语法错误(ELIF),我无法识别

2024-09-23 10:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

score = float(input("Score: "))
if score >= 0 and score <= 1.0:
    elif score >= 0.9:
        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("F")
else: 
    print("Bad Score")

尝试运行此简单代码,但输出给我一个语法错误,我无法识别

输出:

File C:\Python\Aprendizado 2 (PY for Everybody)\2.15.7.py", line 3
    elif score >= 0.9:
       ^
SyntaxError: invalid syntax

Tags: and代码pyinputiffloatelsefile
1条回答
网友
1楼 · 发布于 2024-09-23 10:22:18

一旦进入if块,您需要在其中定义另一个if条件,然后才能访问elif,否则它将出错,因为您试图引用不在块范围内的if

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

if score >= 0 and score <= 1.0:
    if score >= 0.9:
        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("F")
else: 
    print("Bad Score")

相关问题 更多 >