需要帮助制作“赞美生成器”

2024-06-28 11:51:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个简单的恭维生成器,它从两个独立的列表中提取一个名词和一个形容词,然后将它们随机组合在一起。我可以让它自己工作,但试图让第二个词出现会让奇怪的事情发生。我做错什么了?任何投入都会很好。在

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)    

最后的结果使我得到一个双名词。我怎样才能消除重复?在


Tags: inforifrandomwordcountsentencewordst