Skip to main content
 首页 » 编程设计

python之在基本程序中使用 else 得到错误 : unindent does not match any outer indentation level

2025年05月04日20zlslch

当我运行以下代码时,出现错误:“IndentationError: unident does not match any outer indentation level”。

我做错了什么?我在下面包含了我的代码以供引用:

file=open('csquetionseasy.txt','r') 
 
print(file.read(432)) 
 
answersinputeasy=input('enter the letter responding to the correct answer for all 3 questions e.g. BBB') 
 
if answersinputeasy==('BAA'): 
    print('you got 3/3!') 
 
else: 
    if answersinputeasy==('BAB'): 
        print('2/3!') 
        else: 
            if answersinputeasy==('BBB'): 
                print('1/3!') 
                else: 
                    if answersinputeasy==('ABB'): 
                        print('0/3!') 
                        else: 
                            if answersinputeasy==('AAB'): 
                                print('1/3!') 
                                else: 
                                    if answersinputeasy==('AAA'): 
                                        print('2/3!') 
                                        else: 
                                            if answersinputeasy==('ABA'): 
                                                print('1/3!') 

请您参考如下方法:

使用elif 而不是else。当 if 和 elif 语句的计算结果为 false 时,您需要 else 语句来执行某些操作。

if answersinputeasy==('BAA'): 
    print('you got 3/3!') 
elif answersinputeasy==('BAB'): 
    print('2/3!') 
elif answersinputeasy==('BBB'): 
    print('1/3!') 
elif answersinputeasy==('ABB'): 
    print('1/3!') 
elif answersinputeasy==('AAA'): 
    print('2/3!') 
elif answersinputeasy==('ABA'): 
    print('1/3!') 
else: 
    print('Invalid Input') 

此外,如果您想要指示一个代码块,您必须将该 block 的每一行缩进相同的数量,通常是四个空格。