Human Human - 1 month ago 11
Python Question

Use the same Random Generated Number Several Times

I'm writing a code where the user inputs a sentence, inputting one word at a time. When they type 'exit', the code returns "Your original sentence was sentence. Word number (generate random number) is (the word that corresponds to that number)".

For example, the sentence is "This is a cool code", it'll return "Your original sentence was This is a cool code. Word number 3 is a.

Right now, my code gets two different random numbers and words so it'll be like "Word number 2 is this" or something. How should I fix this and get it to work properly?

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)

Answer

You were almost there!

When you do:

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

You're getting a number (in the pleaseWork variable) between zero and len(outputString), which (since outputString is a string) will give you the number of characters in outputString.

Try it out:

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

However, what you really want, is a random index between 0 and the number of items in your sentence list, right? Because when you do this: sentence[pleaseWork] you're using pleaseWork as an index for the sentence list, not for your outputString string.

So, what you're doing here:

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

can be shortened to:

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

Do you see it now? pleaseWork contains a number between 0 and the lenght of outputString. BUT then you're using that number to access your list sentence. What happens if outputString has 100 characters and sentence has only three items? Well... You're quite likely to obtain an integer higher than 3 in that random.randint call. Let's say you get... 50... What happens when you try to access the 50th item a list with three items?

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

So, what you should do is change the range of that randint to the number of items in your sentence list:

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

Full example:

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)