我正在尝试制作一个简单的赞美生成器,它从两个单独的列表中取一个名词和一个形容词并将它们随机组合在一起。我可以自己一个人去工作但是试图让第二个词出现会让奇怪的东西发生。我在这做错了什么?任何输入都会很棒。
import random
sentence = "Thou art a *adj *noun."
sentence = sentence.split()
adjectives = ["decadent", "smelly", "delightful", "volatile", "marvelous"]
indexCount= 0
noun = ["dandy", "peaseant", "mule", "maiden", "sir"]
wordCount= 0
for word in sentence:
if word =="*adj":
wordChoice = random.choice (adjectives)
sentence [indexCount] = wordChoice
indexCount += 1
for word in sentence:
if "*noun" in word:
wordChoice = random.choice (noun)
sentence [wordCount] = wordChoice
wordCount += 1
st =""
for word in sentence:
st+= word + " "
print (st)
最终结果让我成为双重名词。我怎么能摆脱副本?
答案 0 :(得分:0)
您在第二个循环中没有像wordCount
那样递增indexCount
。