Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random sentences in python

I'm very new to python and programming!

I need to write a program that has 4 tuples with 5 elements each. One tuple should have verbs, one tuple should have nouns, one tuple should have adjectives, and one tuple should have adverbs. Then I have to use a randomly generated numbers between 0 and 4 to pick one of the elements from each tuple. This is what I have so far:

import random

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print (num)

Can someone show me what I'm doing wrong?

like image 545
Ashley Glover Avatar asked Apr 29 '15 08:04

Ashley Glover


People also ask

Is there a random letter generator in Python?

Generate a random letter using a string and a random module The string module has a special function ascii_letters which returns a string containing all the alphabets from a-z and A-Z, i.e. all the lowercase and uppercase alphabets.


1 Answers

You can use random.choice within a list comprehension then concatenate the selected list with join:

>>> l=[nouns,verbs,adj,adv]
>>> ' '.join([random.choice(i) for i in l])
'girl runs dirty crazily.'
>>> ' '.join([random.choice(i) for i in l])
'monkey hits clueless occasionally.'
like image 59
Mazdak Avatar answered Oct 09 '22 11:10

Mazdak