Beginner Python Projects: Build a Simple Random Story Generator

Written by mindninjax | Published 2021/02/10
Tech Story Tags: python | python-programming | python-tutorials | projects | tutorial | tutorial-for-beginners | codenewbie | learn-to-code-python | web-monetization

TLDR Beginner Python Projects: Build a Simple Random Story Generator. Python-Projects-for-beginners project. Use Python to build a random story generator using a few lists of phrases. Every time we run our program, a sentence will be randomly formed by picking phrases from those lists. You can add more scenes as lists as per your wish. The complete source code of this project can be found here - the source code is available at PyPyX/Python- Projects-For-Beginners- for- Beginners. The random module comes pre-installed with python hence we don't have to manually install it.via the TL;DR App

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

How Our Story Generator works

Our random story generator will use 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 that 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
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']
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 performs.
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) + '.')
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 -

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

Written by mindninjax | LIVE + LOVE + CODE!
Published by HackerNoon on 2021/02/10