How to build a Random Story Generator using Python?
Last Updated :
26 Mar, 2021
In this section, we are going to make a very interesting beginner-level project of Python. It is a random story generator. The random story generator project aims to generate random stories every time user executes the code. A story is made up of a collection of sentences. We will choose random phrases to build sentences, and hence stories.
Now, the pertinent question is - How we will do so? Its answer is very simple :
- We will first put the elements of the story in different lists.
- Then we will use the random module to select random parts of the story collected in different lists.
- And then concatenate them to make a story.
We will make use of random.choice() function. Before starting, let's see an example of how random.choice() works.
Python3
import random
# list of books is stored in the list -'books'
books = ['Mother', 'Midnight Children', 'My experiments with truth']
# An item from the list 'books' is selected
# by random.choice()
print(random.choice(books))
As we can see, random.choice() function basically selects an item from a list of items.
Following are the steps involved in this Random story generator project.
1. Import the random module, as it is a built-in module of python. So, there's no need to install it manually.
Python3
# Importing random module
import random
2. Define several lists of phrases. Here, we have defined eight lists. We can define more also, it depends totally on our choice.
- Sentence_starter - This list gives an idea about the time of the event.
- character - This list tells about the main character of this story.
- time - This list defines the exact day on which some incident has occurred.
- story_plot - This list defines the plot of the story.
- place - This list defines the place at which the incident occurred.
- second_character - This list defines the second character of the story.
- age - This list defines the age of the second character.
- work - This list tells about the work the second character was doing.
Python3
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by', ' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
3. With the help of random.choice() select an item from each list and concatenate the selected items to generate sentences for the story.
Python3
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot)+
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
Implementation:
Let's try the full implementation with the help of an example.
Python3
# Importing random module
import random
# Defining list of phrases which will help to build a story
Sentence_starter = ['About 100 years ago', ' In the 20 BC', 'Once upon a time']
character = [' there lived a king.',' there was a man named Jack.',
' there lived a farmer.']
time = [' One day', ' One full-moon night']
story_plot = [' he was passing by',' he was going for a picnic to ']
place = [' the mountains', ' the garden']
second_character = [' he saw a man', ' he saw a young lady']
age = [' who seemed to be in late 20s', ' who seemed very old and feeble']
work = [' searching something.', ' digging a well on roadside.']
# Selecting an item from each list and concatenating them.
print(random.choice(Sentence_starter)+random.choice(character)+
random.choice(time)+random.choice(story_plot) +
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
Output:
In the 20 BC there lived a king. One day he was going for a picnic to the mountains he saw a man who seemed to be in late 20s digging a well on roadside.
In this way, we can compile and run this code as many times as we want. And different short stories will be generated.
Similar Reads
Random Binary Tree Generator using Python A binary tree is a tree data structure where each node has at most two children, known as the left child and the right child. A random binary tree is a binary tree that is generated randomly with a certain number of nodes. Random binary trees are commonly used for testing algorithms and data structu
7 min read
How to Generate a Random Password Using Ruby? Generating a random password is a fundamental task in cybersecurity and application development. Creating a secure random password is very easy with the Ruby library. This article will show how to generate a random password in Ruby in Python. Generate a Random Password Using RubyBelow are the code e
2 min read
Story Generator App Using Python In the realm of programming and natural language processing, there's an opportunity to create engaging and creative applications that spark the imagination. One such project is the development of a Story Generator App using Python. In this article, we'll embark on a journey to understand how to buil
4 min read
Python | Random Password Generator using Tkinter With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep th
4 min read
Create a Random Password Generator using Python In this article, we will see how to create a random password generator using Python. Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination
5 min read
Random Singly Linked List Generator using Python In this article, we will learn how to generate a singly linked list using random numbers. Prerequisite: Insertion in Linked List A Singly Linked List is a linear data structure. A linked list is a collection of nodes and in a singly linked list every node contains two things: Data Reference to the n
5 min read
How to generate a unique username using Python A username is a unique name that is mainly used on websites and apps. A username is used to identify a person. For example, if your name is Akshay and you want to create your account on any social media app, here we need a username such that another person can find us. It is not possible to search b
4 min read
Python Script to create random jokes using pyjokes Python supports creation of random jokes using one of its libraries. Let us explore it a little more, Pyjokes is a python library that is used to create one-line jokes for programmers. Informally, it can also be referred as a fun python library which is pretty simple to use. Let us see how you can a
2 min read
How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem
2 min read
How to Pull a Random Record Using Django's ORM? Django's Object-Relational Mapping (ORM) is a powerful tool that allows developers to interact with the database using Python code instead of raw SQL queries. One common task in web development is to pull a random record from the database, whether for displaying a random quote, product, or any other
3 min read