我正在编写代码,从3个单独的文件中提取前缀、根单词和后缀,并将它们放在一起(这是代码中的"finalword"变量).代码工作并生成一个单词.
我想做的是,生成大量单词(比如1000个),而不必反复运行代码.我考虑过使用while循环:
while finalword != 1:
print(finalword)
但所有这些只是打印相同的最终版本,而不是每次打印一个新版本.我如何让这个循环每次打印新的独特单词?
这是我的代码:
import random
# finalword components
file = open("prefixes.py", "r") # opening word list
prefixes = file.read().split("\n") # splitting all the lines
xprefixes = random.choice(prefixes) # getting a random word from the file
file = open("words.py", "r")
words = file.read().split("\n")
xwords = random.choice(words)
file = open("suffix.py", "r")
suffix = file.read().split("\n")
xsuffix = random.choice(suffix)
# final word, which combines words from the lists
finalword = (f'{xprefixes}{xwords}{xsuffix}')
print(finalword)