我想返回从一个文本文件中的单词(这样我就可以最终使从字游戏),但现在我得到的错误获取从文本随机的Word文件
IndexError: string index out of range
这是我的文本文件看起来像
yellow
awesome
barking
happy
dancing
laughing
,这是代码我现在有
进口随机
def generate_the_word(infile):
for line in infile.readlines():
random_line = random.randrange(line[0], line[len(line) + 1])
print(random_line)
def main():
infile = open("words.txt","r")
generate_the_word(infile)
infile.close
main()
我对索引有错误的想法吗?
谢谢你这个完美! –