我将粘贴我的整个代码,以便您可以理解您需要知道的所有内容,即使我知道您可能并不需要这一切。我也要警告你,我对编程很陌生,所以我的代码效率可能非常低,非常混乱,我为此道歉!如果你看到我需要解决的任何其他问题,我将不胜感激。
程序目标摘要:读取一个3个字母的单词的文件,在将其插入带有(已知)3个字母单词列表的文本文档之前,询问该单词是否为3个字母的单词进行验证。 (为了做到这一点,它需要定义找到的3个字母单词的每个字符。(?))
# wdf - word functions
import sys
import time
import re
import copy
import string
from string import ascii_lowercase
from random import *
def is_three_letters(word): # code from ChristianFigueroa @ stackoverflow.com
return bool(re.match(r"[a-zA-Z/d]{3}$", word))
def i_otpt():
f = open("output.txt", "a")
f.write("\n")
f.write("\n")
f.write(">-------------------------------<")
f.write("\n")
f.write("\n")
f.write(time.asctime(time.localtime(time.time())))
f.write("\n")
f.write("----N-E-W--O-U-T-P-U-T--L-O-G----")
f.write("\n")
f.write("\n")
f.close()
def o_otpt():
f = open("output.txt", "a")
f.write("\n")
f.write("----E-N-D--O-U-T-P-U-T--L-O-G----")
f.close()
def otpt(tx):
f = open("output.txt", "a")
f.write(str(tx))
f.write("\n")
f.close()
def otpt2(tx, tx2):
f = open("output.txt", "a")
f.write(str(tx))
f.write(" ")
f.write(str(tx2))
f.write("\n")
f.close()
def otpt3(tx, tx2, tx3):
file = open("output.txt", "a")
file.write(str(tx))
file.write(" ")
file.write(str(tx2))
file.write(" ")
file.write(str(tx3))
file.write("\n")
file.close()
def wd_sc_3(name_of_file):
with open(name_of_file + ".txt") as f:
lines = f.readlines()
wdl = []
x = 0
for line in lines:
words = [word for word in line.split() if len(word) == 3 and all(ch in ascii_lowercase for ch in word)]
x += 1
if x % 2 == 1:
wdl.append(words)
l = 0
lx = wdl[l][0]
dvd = dict(enumerate(lx, 1))
l1 = dvd[0]
l2 = dvd[1]
l3 = dvd[2]
word3(l1, l2, l3)
l += 1
f.close()
v = len(wdl)
print("Found ", v, " words")
n = 0
file = open("read3.txt", "a")
file.write("\n")
while True:
file.write((wdl[n][0]))
file.write("\n")
n += 1
if n >= v:
break
def word3(a, b, c): # Defines a 3 letter word and prints it as one word
wd3 = (a + b + c)
i_otpt()
sys.stdout.write("Word check: ")
otpt2("Word check: ", wd3)
for var in a:
sys.stdout.write(var)
sys.stdout.flush()
for var in b:
sys.stdout.write(var)
sys.stdout.flush()
for var in c:
sys.stdout.write(var)
sys.stdout.flush()
sys.stdout.write("\n")
sd1 = 0
slx = 0
file = open("list_of_words_3.txt", "r")
otpt("\nWord 3 file opened to read.")
while slx <= 250:
file.readline(slx)
for line in file:
otpt2(" -Reading line", (int(slx) + 1))
if wd3 in line:
sd1 = 1
file.close()
print("(-)Word already stored", "\n")
otpt("Word already stored")
otpt("Word 3 file closed.")
break
elif not line.strip():
otpt(" -Empty")
continue
elif wd3 not in line:
slx += 1
continue
elif slx == 250:
file.close()
otpt("Word 3 file full")
otpt("Word 3 file closed.")
break
else:
print("Something went wrong.")
otpt("Something went wrong. (1)")
break
slx = 251
break
file.close()
otpt("Word 3 file closed.")
if slx == 251:
file = open("list_of_words_3.txt", "a")
otpt("Word 3 file opened to add word.")
if sd1 == 0:
print("Is ", wd3, " a word?")
ans = input("---> ")
if ans == "yes":
file.write(wd3)
otpt3("Word ", wd3, " added to file.")
file.write(",\n")
otpt("Word 3 file closed.")
file.close()
sys.stdout.write("(+)New word added: ")
sys.stdout.write(wd3)
sys.stdout.write("!")
otpt3("New word added: ", wd3, "!")
file.close()
if ans == "no":
print("Okay, registering ", wd3, " as not a word")
file.close()
otpt("Opening not a word 3 file")
file = open("list_of_not_words_3.txt", "r")
sly = 0
sd2 = 0
while sly <= 250:
file.readline(sly)
for line in file:
otpt2(" -Reading line", (int(sly) + 1))
if wd3 in line:
sd2 = 1
file.close()
otpt("Word already stored")
otpt("Not a word 3 file closed.")
break
elif not line.strip():
otpt(" -Empty")
continue
elif wd3 not in line:
sly += 1
continue
elif sly == 250:
file.close()
otpt("Not a 3 word file full")
otpt("Not a 3 word file closed.")
break
else:
print("Something went wrong.")
otpt("Something went wrong. (2)")
break
sly = 251
break
file.close()
otpt("Not a 3 word file closed.")
if sly == 251:
file = open("list_of_not_words_3.txt", "a")
otpt("Not a 3 word file opened to add word.")
if sd2 == 0:
if ans == "yes":
file.write(wd3)
otpt3("Word ", wd3, " added to file.")
file.write(",\n")
otpt("Not a word 3 file closed.")
file.close()
otpt3("New word added: ", wd3, "!")
file.close()
if sd2 == 1:
otpt("No need to add word")
else:
otpt("No word added")
if sd1 == 1:
otpt("No need to add word")
else:
print("No word added.")
otpt("No word added")
wd_sc_3("test")
错误:
Traceback (most recent call last):
Found 65 words
File "/Users/ethanmcrae/PycharmProjects/AI/wdf.py", line 222, in <module>
wd_sc_3("test")
File "/Users/ethanmcrae/PycharmProjects/AI/wdf.py", line 90, in wd_sc_3
file.write((wdl[n][0]))
IndexError: list index out of range
Process finished with exit code 1
我意识到这个问题可能需要花费相当多的时间来修复,所以我理解这是否需要一些时间来解决。
答案 0 :(得分:1)
在代码的这一部分(向中间)找到了您的问题
for line in lines:
words = [word for word in line.split() if len(word) == 3 and all(ch in ascii_lowercase for ch in word)]
x += 1
if x % 2 == 1:
wdl.append(words)
l = 0
lx = wdl[l][0]
dvd = dict(enumerate(lx, 1))
l1 = dvd[0]
l2 = dvd[1]
l3 = dvd[2]
word3(l1, l2, l3)
l += 1
在for循环中,你将L设置为0,因此只设置了wdl [0] [n] 它永远不会去wdl [1] [n]