私は、2つの別々のリストから名詞と形容詞を取り、それらをランダムに結合する簡単な補完ジェネレータを作成しようとしています。私は自分自身で仕事をすることができますが、第二の言葉を出そうとすると奇妙なことが起こります。私はここで間違って何をしていますか?どんな入力も素晴らしいでしょう。"補完ジェネレータ"を作成するのに助けが必要
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 =" Thou art {{} {} "の形式に置き換えることができます(format(random.choice(形容詞)、random.choice(名詞))'。あなたはC言語でこれを実装するよりも多くの行をPythonで書くことができました。 – ShadowRanger