首页 > 解决方案 > 在基本程序中使用 else 得到错误:unindent 不匹配任何外部缩进级别

问题描述

当我运行以下代码时,我收到一个错误:“IndentationError:unident 不匹配任何外部缩进级别”。

我究竟做错了什么?我在下面包含了我的代码以供参考:

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!')

标签: pythonif-statementsyntax-errorindentation

解决方案


使用elif而不是else. 当和语句评估为假时,您需要else语句来做某事。ifelif

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')

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


推荐阅读