How to make a random story generator in Python

Markus Urban
5 min readJul 3, 2020

--

Hi! I’m a young Python developer and today I want to show you how you can make a simple Python program that generates a random story like this:

A few years ago, a rabbit named Ali, that lived in Spain, went to the university and made a lot of friends.

Your small story won’t be a bestseller, but you are going to learn how to create an easy Python program and you will be able to make this program much better and write a complete story.

Let’s start!

In this tutorial we are going to use a Windows based computer with Python 3.8 installed. To begin, let’s open IDLE to write our program:

Once we opened it, let’s create a new project that we are going to call random-story.py

We create a new file
We save it

Now let’s start writing code. We are going to use basic functions such as print or random because this is a very basic tutorial.

To begin, let’s start importing the libraries that we need. In this case, we are only going to use the random library. To use the library in our program, write:

Now let’s run our program. To do that, go to run and click run module.

We select the option Run
And click Run module

If you don’t seee any errors, as you can see in the following image, then the random library is alredy installed in our device and we can continue:

Our program worked correctly

If you find an error that says ‘ModuleNotFoundError: No module named “Random” ’. Then you must install the random library.

To do that, open the command prompt (cmd) and write ‘pip install random’. You can see how to do it in the following images:

We open the CMD
And write ‘pip install random’

Click enter and the random library will be installed in your computer.

Now let’s contnue writing our program. We are going to make different lists with different words and names that will randomly be selected.

Let’s create the first variable, named ‘when’:

You can change the words to make the story as you want

To continue, let’s create the variable with different types for our character:

Now let’s create another variable with names for our character:

Now we must create a variable with locations were our character is going to live:

To continue, let’s create another list that tells us what our character did (were he/she went):

And, to finish, this last variable to know what happened:

Now we have all the lists with different situations. Now let’s randomly select one item of every list and make a story of it.

We are going to use the random module and the print function:

In this code, the random.choice() function gets a random item of the list that we defined before (we put inside the parenthesis the list were he must select the random word).

The + sign joins all this words and, when we run our program, the result is going to be a selection of random words from our lists put toguether:

Yesterdaya rabbitMiriamVeniceuniversityfound a secret key

Your program is going to select other words because it’s random

As you can see, our story isn’t finished. We must ahh spaces, commas and other words to make a story with a bit of sense. To do that, we add to the code that we alredy have spaces, commas and other words like this:

In this code, the print function is going to show our final story on screen. Let’s see what our final program does:

Last night, a turtle that lived in Barcelona, went to the cinema and made a lot of friends

I had a vision: this is going to be the first chapter of my new book

Lets run our program again:

Yesterday, an elephant that lived in Venice, went to the cinema and wrote a book

Oh! This is going to be the second chapter! And then the elephant and the turtle will fall in love

Let’s try again:

A long time ago, a mouse that lived in Venice, went to the laundry and solved a mistery

I can’t believe it! She solved the mistery of the secret love between the elephant and the turtle!

Here you can see an image of what happened when we run the program in our computer:

The output of our program

As you have probably seen, this is a very basic program and the stories it does aren’t very complicated. Here I show you the complete code and propose a few ways to make it more complex:

How to make your program better:

1- You could make the lists bigger

2- You could try to make the program select all the words of the story using, for example, a random words module

3- You could add conditional statements to the story (for example, if the main character lives in Spain then he visits Barcelona)

Thanks for reading this article. If you liked it, let me know sending a clap.

I hope you enjoyed this little project. See you soon in another post, Markus

--

--

Markus Urban
Markus Urban

Written by Markus Urban

I'm a young programmer who likes creating new things.

Responses (4)