完形填空求解器不起作用

时间:2018-08-18 13:37:16

标签: python python-3.x

我已经使用python一段时间了,我有一个文件,该文件是英文字母,每个单词都位于单独的行中,并且已经消除了所有错误,但不再输入任何内容。只要您解释它是如何工作的,我都会向您提供任何帮助和代码改进。如果您要查找的文件是here

f = open("C:\\my folders\\brit-a-z.txt", 'r')

print("Type in your cloze word like this: cl-z-")
def cloze(word):
    for line in f:
        match = True
        if len(line) == len(word):
            for i in range(len(word)):
                if not word[i]  == "-":
                    if line[i] == word[i]:
                        match == False
            if match == True:
                print(line)

while True:
    cloze(input())

1 个答案:

答案 0 :(得分:1)

遍历文件中的第一个单词后,文件将关闭。您需要保存f。例如:

with open("C:\\my folders\\brit-a-z.txt", 'r') as f:
      f = f.read().splitlines()