我试图将不正确的单词附加到列表中,但是当我在函数外部打印列表时,该列表为空,而当我在函数内部打印列表时,它将为每个单词打印一个列表。如何在程序末尾使用不正确的单词将列表打印一次?
文件1: 这是我的拼写检查程序
文件2:dis是我的spll cheker程序
因此,应该在列表中添加3个错误的单词
word_list = []
if cmdlength != 2:
print ("Usage error, expected 2 args got " + str(cmdlength))
exit()
else:
try:
f = open(sys.argv[1])
f.close()
except FileNotFoundError:
print("File does not exist")
exit()
try:
ff = open(sys.argv[2])
ff.close()
except FileNotFoundError:
print("File does not exist")
exit()
word = ""
with open(sys.argv[1],"r") as fh:
while True:
ch=fh.read(1)
if ch == " " or ch == "\n" or ch == ":" or ch == ".":
with open(sys.argv[2],"r") as fh2:
def check_word(word,fh2,word_list):
lines = fh2.readlines()
for line in lines:
x= re.search(word,line)
if x:
#correctwords
print(word + ": " + "0")
#count += 1
else:
#incorrect words
print(word, ": " , "1")
word_list.append(word)
#count2 += 1
check_word(word,fh2,word_list)
word = ''
else:
word += ch
if not ch:
print(word)
print("End of file")
print(word_list)
break
答案 0 :(得分:0)
该函数缺少return
语句来返回word_list
。
这应该起作用
...
def check_word(word,fh2,word_list):
lines = fh2.readlines()
for line in lines:
...
word_list.append(word)
#count2 += 1
return word_list
word_list = check_word(word,fh2,word_list)