vendredi 6 décembre 2019

How can I insert the same word from a list that was randomly chosen earlier in Python?

I am creating a random sentence generator for a course assignment. I have everything pretty much figured out but there is a spot in the sentence template we have to use where I need to first randomly choose a word, then repeat that randomly chosen word again in the string. The problem occurs after random.choice(place) when I need to immediately use whatever word was chosen again. How can I grab that same word that was randomly chosen and use it again?

import random

verb = ["dancing", "driving", "running"]
place = ["rain", "forest", "city"]
adjective = ["heavy", "long", "hard"]

count = 1

while count > 0:

    generate = int(input('Press 1 to generate a new sentence or 2 to exit.\n'))

    if generate == 1:
        print('I like to go',random.choice(verb),'in the',random.choice(place),',but',place,'should not be too',random.choice(adjective),'.')
        count = 1

    else:
        print('Ok, Goodbye!')
        count = 0



Aucun commentaire:

Enregistrer un commentaire