10

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?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Ashley Glover
  • 143
  • 1
  • 2
  • 5
  • 3
    You can use that random number to select the corresponding element from each of the tuples. eg: `print nouns[num]` https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences – ρss Apr 29 '15 at 08:57
  • 3
    A better solution would be to use `random.choice(nouns)`… but if your assignment requires you to use `random.randrange`, then you hav eto do it the way @ρss suggests. – abarnert Apr 29 '15 at 08:59
  • 1
    I agree with @abarnert – ρss Apr 29 '15 at 09:00

8 Answers8

14

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.'
Mazdak
  • 105,000
  • 18
  • 159
  • 188
4

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]
Avión
  • 7,963
  • 11
  • 64
  • 105
  • 4
    Note that with this method, there are only 5 possible sentences, instead of the 625 possible sentences if you pick a random number for each word. – Eric Duminil Jul 29 '19 at 14:13
3

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)])
mr.plow
  • 43
  • 7
1

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))
GHugo
  • 2,584
  • 13
  • 14
1

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)
Spirine
  • 1,837
  • 1
  • 16
  • 28
1

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] 
Subham
  • 397
  • 1
  • 6
  • 14
1

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)])
wovano
  • 4,543
  • 5
  • 22
  • 49
BugV VZero
  • 9
  • 2
  • 9
0
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])
Chiyaan Suraj
  • 1,021
  • 2
  • 13
  • 27
  • Note that with this method, there are only 5 possible sentences, instead of the 625 possible sentences if you pick a random number for each word (same problem with [this other answer](https://stackoverflow.com/a/29938988/16775594). – Sylvester Kruin Nov 11 '21 at 00:36