Human
Human

Reputation: 13

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)

Upvotes: 0

Views: 69

Answers (3)

Savir
Savir

Reputation: 18418

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 length 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 bigger than 2 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)

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49320

You are using the number of characters in the sentence to generate an index for the words in the sentence. Change wordCount = int(len(outputString)) to wordCount = len(sentence) - 1 to get an appropriate index.

Upvotes: 0

Alex Hall
Alex Hall

Reputation: 36013

It doesn't get two different random numbers or words since you only called randint once. The problem is two things:

  1. wordCount is currently the number of characters in the string. Use len(sentence) instead.
  2. randint is inclusive of its upper bound (unlike range which excludes it so that you don't have to think about stuff like this) meaning that random.randint(0,wordCount) sometimes returns wordCount and so pleaseWork == wordCount == len(sentence) (assuming you do the above) is a possiblity, and sentence[len(sentence)] is bound to throw an error. Use random.randint(0,wordCount-1).

Upvotes: 0

Related Questions