【发布时间】:2020-05-07 02:08:07
【问题描述】:
我在这个程序中有三个列表。 'library' 包含大量文本,'references' 包含在references 中相互跟随的单词列表,而'possibles' 会查看每次'references' 中的每个项目一起出现的时候,以便它可以得到以下单词。
程序获取“引用”的长度,因此它知道要在“库”中检查多少项目,但是检查引用是否有 2 个项目的代码返回“意外缩进”,我不能找出原因。即使我删除缩进,它也会返回错误。如果它没有缩进,则会出现错误,因为它会破坏其余代码。
下面是整个函数。除了缩进错误之外,整个程序都能正常工作。
def possibles_compile():
compile = 'yes'
global temp_possibles
temp_possibles = []
while compile != 'no':
if len(reference) == 1:
for i in range(library_length):
try:
if library[i] == reference[0]:
try:
temp_possibles.append(library[i + 1])
except IndexError:
temp_possibles.append(library[0])
elif len(reference) == 2:
for i in range(library_length):
if library[i] == reference[0]:
try:
if library[i + 1] == reference[1]:
try:
temp_possibles.append(library[i + 2])
except IndexError:
temp_possibles.append(library[0])
except IndexError:
if library[0] == reference[1]:
temp_possibles.append(library[1])
elif len(reference) == 3:
for i in range(library_length):
if library[i] == reference[0]:
try:
library[i + 1] == reference[1]:
except IndexError:
if library[0] == reference[1]:
if library[1] == reference[2]:
temp_possibles.append(library[2])
else:
try:
if library[i + 2] == reference[2]:
try:
temp_possibles.append(library[i + 3])
except IndexError:
temp_possibles.append(library[0])
except IndexError:
if library[0] == reference[2]:
temp_possibles.append(library[1])
elif len(reference) == 4:
for i in range(library_length):
if library[i] == reference[0]:
try:
library [i + 1] == reference[1]:
except IndexError:
if library[0] == reference[1]:
if library[1] == reference[2]:
if library[2] == reference[3]:
temp_possibles.append(library[3])
else:
try:
library[i + 2] == reference[2]:
except IndexError:
if library[0] == reference[2]:
if library[1] == reference[3]:
temp_possibles.append(library[2])
else:
try:
if library[i + 3] == reference[3]:
try:
temp_possibles.append(library[i + 4])
except IndexError:
temp_possibles.append(library[0])
except IndexError:
if library[0] == reference[3]:
temp_possibles.append(library[1])
compile = 'no'
return 'done'
【问题讨论】:
-
在您的第一个
if块中,您有一个try语句没有匹配的except(或finally)(这是您问题中代码的第 7 行)。 -
然后你有一些放错位置的冒号。参见例如第 31 行。一般来说,如果您使用带有语法检查功能的编辑器,您的生活会更轻松,因为它可以将这些错误指出给您。
-
我不能相信我没听懂。我读了大概 20 遍,却错过了非常感谢
-
欢迎来到 SO!查看tour。很高兴你想通了 :) 将来你需要创建一个minimal reproducible example。
标签: python if-statement indentation