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?

3

8 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.'

The easiest way for your understanding:

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 nouns[num] + ' ' + verbs[num] + ' ' + adv[num] + ' ' + adj[num]
1

For a similar function to the accepted answer that returns all possible permutations:

import random

def make_random_sentence():
  nouns = ["puppy", "car", "rabbit", "girl", "monkey"]
  verbs = ["runs", "hits", "jumps", "drives", "barfs"]
  adv = ["crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally."]
  adj = ["adorable", "clueless", "dirty", "odd", "stupid"]

  random_entry = lambda x: x[random.randrange(len(x))]
  return " ".join([random_entry(nouns), random_entry(verbs), random_entry(adv), random_entry(adj)])

If you want to select always the same index for each tupple, then try something like this:

import random
idx = random.randrange(0, 5)
print ("%s %s %s %s"%(nouns[idx], verbs[idx], adv[idx], adj[idx]))

If you want to get random elements for each tupple, the following should work:

import random
print("%s %s %s %s"%(random.choice(nouns), random.choice(verbs), random.choice(adv), random.choice(adj))

I would do as follows, using random.choice because it's a better choice than random.randrange...

words = (adj, nouns, verb, adv)
sentence = "".join(choice(word) for word in words)

random.choice is to select a random thing from a list, tuple or similar container. What the last line does is to select one random thing from each list, and make a new list from the choices. Then that list is printed. And being in a loop, this is done X number of times, whatever number the user inputs.

from random import choice 

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

# asking user as to how many sentences he would like to generate 
for _ in range (int (input ("Enter integer value :"))): 
    print(list(map(choice, [nouns, verbs, adv, adj])))
Enter integer value :2
['car', 'barfs', 'dutifully.', 'odd']
['rabbit', 'runs', 'occasionally.', 'clueless']

[Program finished] 
0

This is a simple one. If you want multiple sentences possible, use this:

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

print(nouns[random.randrange(0,5)] + ' ' + verbs[random.randrange(0,5)] + ' ' 
+ adv[random.randrange(0,5)] + ' ' + adj[random.randrange(0,5)])
num = random.randrange(0,5)   
print (num)

Just gives you the random number you generated. You need to acess the tuples using that random number you generated like this.

print(nouns[num])
print(verbs[num])
print(adv[num])
print(adj[num])
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest