Python 为什么此elif语句返回语法错误?

Python 为什么此elif语句返回语法错误?,python,Python,错误是 File "Scantron.py", line 28 elif len(test_answers) != len(test_key): ^ SyntaxError: invalid syntax 命令以非零状态1退出 def grade_scantron(test_answers, test_key): right = 0 for i in test_answers: if test_answers[i] == test_key

错误是

File "Scantron.py", line 28

    elif len(test_answers) != len(test_key):
       ^
SyntaxError: invalid syntax
命令以非零状态1退出

def grade_scantron(test_answers, test_key):
    right = 0
    for i in test_answers:
        if test_answers[i] == test_key[i]:
            right += 1
        return right
        elif len(test_answers) != len(test_key):
            return -1 

从函数返回右键后的
elif
语句将不起作用,因此会出现错误。 您必须编辑代码,并在
if
语句中缩进
return right

def grade_scantron(test_answers, test_key):
    right = 0
    for i in test_answers:
        if test_answers[i] == test_key[i]:
            right += 1
            return right                  # change here
        elif len(test_answers) != len(test_key):
            return -1 

缩进if循环中的第27行,
return right
if test_answers[i]==test_key[i]:请为代码添加更多上下文,我认为您应该添加整个if语句。并相应地格式化您的问题