DEV Community

loading...
Cover image for How to build a Random Story Generator using Python

How to build a Random Story Generator using Python

mindninjax profile image Rishabh Singh ⚡ ・2 min read

Hey amazing people, today let's build a Random Story Generator using Python.

Alt Text

How Story Generator works?

Our random story generator will be provided with a few lists of phrases and every time we run our program, a sentence will be randomly formed by picking phrases from those lists. Again it's a fun and easy project so let's get into coding.

Let's Code

For this project, as we are going to randomly pick phrases, we need a module which can make our job easy. Any guesses which module I mean?

The random module! random module comes pre-installed with python hence we don't have to manually install it. Let's import it into our project.

import random
Enter fullscreen mode Exit fullscreen mode

Now it's time to define our lists which will contain random phrases.

when = ['A long time ago', 'Yesterday', 'Before you were born', 'In future', 'Before Thanos arrived']
who = ['Shazam', 'Iron Man', 'Batman', 'Superman', 'Captain America']
went = ['Arkham Asylum', 'Gotham City', 'Stark Tower', 'Bat Cave', 'Avengers HQ']
what = ['to eat a lot of cakes', 'to fight for justice', 'to steal ice cream', 'to dance']
Enter fullscreen mode Exit fullscreen mode

Here we have defined 4 different lists but you can do more as per your choice. Also for phrases, I am using some random superhero theme phrases but again feel free to use your own.

Here -

  • when - this list contains the time of our story means when it took place.
  • who - this list contains the main characters of our story.
  • went - this list contains some places where our main character visits
  • what - this list contains some activities our main character perform.

Again, feel free to customize this part the way you like. You can add more scenes as lists as per your wish.

Now let's print the final output to see how our story turns out to be.

print(random.choice(when) + ', ' + random.choice(who) + ' went to ' + random.choice(went) + ' ' + random.choice(what) + '.')
Enter fullscreen mode Exit fullscreen mode

Here we are making use of random.choice() function to randomly pick a phrase from the given list. We are also making use of + operator to concatenate (or combine) all the phrases so they come together & form a story.

Along with this, we are also adding a few strings in middle like spaces and commas to help our sentences appear good.

Here we go we did it!

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Discussion (2)

pic
Editor guide
Collapse
surajondev profile image
Suraj Vishwakarma

Gone try it soon and nice article bro.

Keep up the good thing.

Collapse
mindninjax profile image
Rishabh Singh ⚡ Author

Thanks, bro! Just helping the community learn more :D