Story Generator with Python

 Story Generator with Python

Do you think the most complex use of the random module in Python is random sampling? No, we can also generate random stories or anything beyond that using the random module. In this article, I’ll walk you through how to create a story generator with Python.

Story Generator with Python

Our task is to generate a random story every time the user runs the program. I will first store the parts of the stories in different lists, then the Random module can be used to select the random parts of the story stored in different lists:

Code:

import random

when = ['A few years ago', 'Yesterday', 'Last night', 'A long time ago','On 15th Aug']

who = ['a rabbit', 'an elephant', 'a mouse', 'a turtle','a cat']

name = ['srinu', 'tarunu','mallesh', 'swamy', 'pavan']

residence = ['hyderabad','warangal', 'vijayawada', 'guntur', 'karminagar']

went = ['cinema', 'university','seminar', 'school', 'park']

happened = ['made a lot of friends','Eats a burger', 'found a secret key', 'solved a mistery', 'wrote a book']

print(random.choice(when) + ', ' + random.choice(who) + ' that lived in ' + random.choice(residence) + ', went to the ' + random.choice(went) + ' and ' + random.choice(happened))

Output:





No comments

Powered by Blogger.