我試圖做一個簡單的恭維式發電機,它將兩個單獨的列表中的名詞和形容詞隨機組合在一起。我可以自己做一個工作,但試圖讓第二個詞出現會讓奇怪的事情發生。我在這裏做錯了什麼?任何輸入都會很棒。需要幫助製作一個「恭維發電機」
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)
最終結果讓我感到雙重名詞。我將如何擺脫重複?
什麼是奇怪的東西? – galfisher
我的印刷陳述中收到了一個不理想的結果:少女藝術是一個令人愉快的*名詞。 –
所以,只要提及,基本上所有這些代碼都可以用'st =「你是一個{} {}」格式(random.choice(形容詞),random.choice(名詞))來代替'。你已經設法在Python中編寫更多的行,而不是用C語言實現它。 – ShadowRanger