私は一度に1単語ずつ入力して文章を入力するコードを書いています。 「exit」と入力すると、「あなたの元の文章は文章であり、ワード番号(乱数を生成する)は(その番号に対応する単語)」です。例えば同じ乱数を何回か使用する
は、文は、それはあなたの原文はこれがクールなコードであるた」戻ります。ワード数3である。
を今、私のコードは2を取得し「これはクールなコードである」です異なる乱数との言葉、それはあなたが文の文字数を使用しているか何か。どのように私はこの問題を解決し、それが正常に動作してもらう必要がありますか?
print ('Think of a sentence')
print ('Type the sentence one word at a time pressing enter after each word')
print ("When you have finished the sentence enter 'exit'")
print ('')
sentence = []
while True:
word = input('')
print ('Accepted', word)
if word == 'exit':
print ('')
print ('Your original sentence was')
outputString = " ".join(sentence)
print (outputString)
wordCount = len(outputString.split())
pleaseWork = (random.randint(0,wordCount))
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork]))
break
sentence.append(word)