我试图制作一个简单的恭维生成器,它从两个独立的列表中提取一个名词和一个形容词,然后将它们随机组合在一起。我可以让它自己工作,但试图让第二个词出现会让奇怪的事情发生。我做错什么了?任何投入都会很好。在
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)
最后的结果使我得到一个双名词。我怎样才能消除重复?在
第二个循环中的
wordCount
与第一个循环中的indexCount
不同。在相关问题 更多 >
编程相关推荐