2016-10-27 3 views
0

私は一度に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) 

答えて

1

あなたはほぼそこにいました。

pleaseWork = (random.randint(0,wordCount)) 

あなたはあなたoutputStringの文字数を与える(outputStringが文字列であるため)これ、ゼロとlen(outputString)間(pleaseWork変数で)番号を取得している:あなたはない

それを試してみてください:

>>> len("Hello, my name is BorrajaX") 
26 

をしかし、あなたが本当に欲しいもの、右、0とあなたのsentenceリスト内の項目の数との間のランダムな指標でありますか?あなたがこれを行うとき:sentence[pleaseWork]sentenceのリストのためのインデックスとしてpleaseWorkを使用していて、outputStringの文字列ではないからです。

だから、あなたはここで何をやっている:

pleaseWork = random.randint(0, len(outputString)) 
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork])) 

あなたは今、それを参照しています:

wordCount = int(len(outputString)) 
pleaseWork = (random.randint(0,wordCount)) 
print('Word number ',pleaseWork,' is ', (sentence[pleaseWork])) 

はに短縮することができますか? pleaseWorkは、0と、outputStringとの間の数を含む。 BUTあなたのリストにアクセスするにはその番号を使用していますsentenceoutputStringの文字数が100文字で、sentenceの文字数が3つの場合はどうなりますか?まあ...そのrandom.randintコールでは、2より大きい整数を取得する可能性が非常に高いです。 50 ... 50番目のアイテムに3つのアイテムのリストを表示しようとするとどうなりますか?

wordCount = int(len(sentence)) 
    pleaseWork = (random.randint(0, wordCount)) 

全例:

def sentenceSplit(): 
    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 = raw_input('') 
    print ('Accepted', word) 
    if word == 'exit': 
     print ('') 
     print ('Your original sentence was') 
     outputString = " ".join(sentence) 
     print (outputString) 
     wordCount = int(len(sentence)) 
     pleaseWork = (random.randint(0, wordCount)) 
     print('Word number ', pleaseWork, ' is ', (sentence[pleaseWork])) 

     break 
    sentence.append(word) 

>>> [1, 2, 3][50] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IndexError: list index out of range 

だから、何あなたがすべきことはあなたのsentenceリスト内のアイテムの数とそのrandintの範囲を変更で

0

「数2は、このワード」のようになりますので、文内の単語のインデックスを生成するwordCount = int(len(outputString))wordCount = len(sentence) - 1に変更して適切なインデックスを取得します。

0

randintを1回だけ呼び出したので、2つの異なる乱数または単語を取得することはありません。問題は2つあります。

  1. wordCountは現在文字列の文字数です。代わりにlen(sentence)を使用してください。
  2. randintその上限が含まれており(あなたはこのようなものを考える必要がないようrangeとは違って、それを除外した)(あなたが上記の操作を行うと仮定)random.randint(0,wordCount)は時々wordCountのでpleaseWork == wordCount == len(sentence)を返すことを意味はpossiblityあり、 sentence[len(sentence)]がバインドされてエラーがスローされます。 random.randint(0,wordCount-1)を使用してください。
関連する問題