python - 在基本程序中使用 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!')
解决方案
使用elif
而不是else
. 当和语句评估为假时,您需要else
语句来做某事。if
elif
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')
此外,如果要指示代码块,则必须将块的每一行缩进相同的数量,通常是四个空格。
推荐阅读
- android - Android,使用拖放在pdf上附加电子签名
- java - 在spring mvc和hibernate中将数据从控制器发送到视图的问题
- javafx - JavaFX - 重叠窗格
- windows - 尝试包装 CALL 函数失败
- zsh - 排版:-A:尝试在 ZSH 中创建关联数组时出现无效选项错误
- java - PHP 没有从带有 AsyncTask 的 POST 请求中接收数据
- java - 外部存储:无法查询我有读取权限的文件
- coq - SsrReflect 和 setoid 重写
- spring-boot - Spring Integration JMS - Spring 代码确实在 RuntimeException 上提交
- awk - 比较不同文件中的特定列并打印不匹配的列