user10613120
user10613120

Reputation: 29

How to choose randomly in Python

I have to write a code in python that chooses a word from 7 lists (total of 7 words) and then runs a requested number of lines to form a "poem". Each line of the "poem" is meant to be a different combination of the 7 lists. Any ideas of how to get the program to run different combinations? Mine just runs the same line the number of times I asked:

people=['Amir', 'Itai', 'Sheli','Gil','Jasmin','Tal','Nadav']
verbs = ['talks', 'smiles', 'sings', 'listens', 'eats', 'waves', 'plays', 'swims']
Adverbs =['slowly',  'quickly', 'solemnly', 'nicely', 'beautifully']
Prepositions=['to a', 'with a' ,'towards a', 'at a' ,'out of a']
Adjectives =['white', 'blue', 'green', 'small', 'large', 'yellow', 'pretty', 'sad']
Animated=['fish', 'parrot', 'flower', 'tree', 'snake']
Inanimated=['chair', 'lamp', 'car', 'ship', 'boat']

x=eval(input("How many lines are in the poem?"))
y=(random.choice(people), random.choice (verbs) ,random.choice(Adverbs) ,random.choice(Prepositions) ,random.choice(Adjectives) ,random.choice(Animated+Inanimated))
for i in range (x):
   if (x< 10):
        print (y)

Upvotes: 1

Views: 68

Answers (2)

Mohammad reza Kashi
Mohammad reza Kashi

Reputation: 369

I think this can help you:

import random

people=['Amir', 'Itai', 'Sheli','Gil','Jasmin','Tal','Nadav']
verbs = ['talks', 'smiles', 'sings', 'listens', 'eats', 'waves', 'plays', 'swims']
Adverbs =['slowly',  'quickly', 'solemnly', 'nicely', 'beautifully']
Prepositions=['to a', 'with a' ,'towards a', 'at a' ,'out of a']
Adjectives =['white', 'blue', 'green', 'small', 'large', 'yellow', 'pretty', 'sad']
Animated=['fish', 'parrot', 'flower', 'tree', 'snake']
Inanimated=['chair', 'lamp', 'car', 'ship', 'boat']

while True:
    x=eval(input("How many lines are in the poem?"))
    if x == 0:
        break
    for i in range (x):
        if (x< 10):
            y = (random.choice(people), random.choice(verbs), random.choice(Adverbs), random.choice(Prepositions),random.choice(Adjectives), random.choice(Animated + Inanimated))
            print (y)

Upvotes: 0

lxop
lxop

Reputation: 8625

You have the right idea, but you just need to re-evaluate the random choice each time:

people=['Amir', 'Itai', 'Sheli','Gil','Jasmin','Tal','Nadav']
verbs = ['talks', 'smiles', 'sings', 'listens', 'eats', 'waves', 'plays', 'swims']
Adverbs =['slowly',  'quickly', 'solemnly', 'nicely', 'beautifully']
Prepositions=['to a', 'with a' ,'towards a', 'at a' ,'out of a']
Adjectives =['white', 'blue', 'green', 'small', 'large', 'yellow', 'pretty', 'sad']
Animated=['fish', 'parrot', 'flower', 'tree', 'snake']
Inanimated=['chair', 'lamp', 'car', 'ship', 'boat']

x=eval(input("How many lines are in the poem?"))
for i in range (x):
    y=(random.choice(people), random.choice (verbs) ,random.choice(Adverbs) ,random.choice(Prepositions) ,random.choice(Adjectives) ,random.choice(Animated+Inanimated))
    if (i < 10):
        print (y)

Upvotes: 1

Related Questions