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.'
Answer from Kasravnd on Stack OverflowYou 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]
need help with random sentence generator
HELP - Phyton Paragraph Generator
I took a coaching session so that you don't have to
Quillbot: An AI that can reword and restructure sentences.
Videos
I'm learning Python since this August and want to do random sentence generator, but I want to implement some logic to it (like present, past and continuous tenses and some logic that it knows which words to use with the given noun or verb).
Since last night I am banging my head and cannot come up with algorithm to do that. I use dictionaries for my data like this.
nouns = {'name' : 'guitar', 'tag': 'music'}, etc..
I include some kind of tags so I can use the word accordingly.
Got the idea from this guy : http://lomacar.github.io/Random-Sentence-Generator/?mode=scroll
So my question is how can I best implement this kind of stuff into my code. I really struggle when I need to come up with an algorithm like this. I solve problems all the time but in much smaller scale and there's explanation what to do.
How would I approach this kind of task?
Ps. Sorry for my format, I am writing from my phone.