Write a Python program on story generator
Generally in story there same character or place or same mission and same condition right ?
Then you thought how we use this to write a program or story to right ? but in python we can do this type of task with the help of python module .
What is a python module ?
The module indicates that the code is already stored in a Python database, so we can use it for our purposes.
When it is no longer necessary, we can reuse the same code or module in other programs because it is already declared in a Python database or saved there.
A file containing Python code is called a Python module. It can contain runnable code in addition to defining variables, classes, and functions. You can refer to any file in Python as a module.Using modules is an excellent technique to make your code more reusable and organized.
You must import a module into your Python script in order to use it. To accomplish this, the import statement is used.
According to program requirements, we are importing this module into our code for your usage.
Numerous modules are included in the standard library of Python. We refer to them as built-in modules.
Below are the bulit-in module
- Math Module
- datetime module
- OS module
- sys Module
- Random module
Advantages of built-in modules in Python
Reduced Development Time
Built-in modules in Python are made to perform various tasks that are used without installing external module or writing the lengthy code to perform that specific task. So, developers used them according to their convenience to save the time.
Optimized Performance
Some built-in modules are optimized for performance, using low-level code or native bindings to execute tasks efficiently.
Reliability
These modules have been rigorously tested and have been in use by the Python community for years. As a result, they are typically stable and have fewer bugs compared to new or less well-known third-party libraries.
Consistency
Built-in modules provide consistent way to solve problems. Developers familiar with these modules can easily understand and collaborate on codebases across different projects.
Standardization
As these modules are part of the Python Standard Library, they establish a standard way of performing tasks.
Documentation
Python’s official documentation is comprehensive and includes detailed explanations and examples for built-in modules. This makes it easier to learn and utilize them.
Maintainability
Built-in modules are maintained by the core Python team and community, regular updates, bug fixes, and improvements are to be expected, ensuring long-term viability.
Reduced Risk
Using third-party libraries can introduce risks, like discontinued support or security vulnerabilities.
Random Module:
Basically, random module is used to choose random elements or numbers from given objects.
As long as the user provides the same input using the random module, they will have the option to provide a variety of conditions or numbers using that element module.
The “random” module in Python is used to generates random numbers and provides the functionality of various random operations such as ‘random.randint()’, ‘random.choice()’, ‘random.random()’, ‘random.shuffle()’ and many more.
random.random(): Returns a random float number between 0.0 to 1.0. The function doesn’t need any arguments
import random
random.random()
0.755173688207591 # every time of output has been changed
Other functions in random module are described here:
random.randint(): Returns a random integer between the specified integers
import random
random.randint(1,100)
18
random.randint(1,100)
19
random.randrange(): Returns a random element from the range created by start, stop and step arguments. The start , stop and step parameters behave similar to range() function.
random.randrange(1,10)
6
random.randrange(1,10,2)
9
random.randrange(0,101,10)
60
# validate otp (system generated otp is dynamic )
from random import randint
system_generated_otp = randint(1111,9999)
print(“your otp is : “,system_generated_otp)
Get Aditi Mishra’s stories in your inbox
Join Medium for free to get updates from this writer.
user_input= int(input(“Enter your OTP: “))
if system_generated_otp == user_input:
print(“OTP is valid , transaction processed”)
else:
print(“Wrong OTP , please try again”)
random.choice(): Returns a randomly selected element from a sequence object such as string, list or tuple. An empty sequence as argument raises IndexError
import random
random.choice(‘computer’)
‘p’
random.shuffle(): This functions randomly reorders elements in a list.
numbers=[12,23,45,67,65,43]
random.shuffle(numbers)
numbers
[23, 12, 43, 65, 67, 45] #output
Random story generator project generates random stories every time the code is executed. A story is a collection of sentences. Random phrases are chosen to build the sentences.
What is the best way to do this? The answer is simple:
- First, we will list the elements of the story.
- Next, we will use the random module to select random parts of the story.
- Then concatenate them to form a story.
import random #import rabdome moudle for use in this story
When= [‘A 1000 years ago’, ‘ In the night’, ‘Once upon a time’] #
character = [‘ there lived a king.’,’ there was a man named ram .’,
‘ a simple man.’]
time = [‘ One day’, ‘ One full-moon night’, ‘ might in the night’]
story_plot = [‘ he was passing by’, ‘ he was going for a hunting ‘]
place = [‘ the mountains’, ‘ the garden’, ‘ the village’,’ in small city ‘]
second_character = [‘ he saw a man’, ‘ he saw a young lady’,’ he saw a old farmar’]
age = [‘ who seemed to be in late 20s’, ‘ who seemed very old and feeble’ ]
work = [‘ searching something.’, ‘ digging a well on roadside.’]
print(random.choice(When)+random.choice(character),’ \n ‘+
random.choice(time)+random.choice(story_plot)+
random.choice(place)+random.choice(second_character)+
random.choice(age)+random.choice(work))
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.
import random #import rabdome moudle for use in this story
2. Define several lists of phrases. Here, we have defined eight lists. We can define more also, it depends totally on our choice.
- when — 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.
3. With the help of random.choice() select an item from each list and concatenate the selected items to generate sentences for the story.
import random
time = [‘2 years ago’,’today’,’1000 year ago’, ‘yesterday’]
person_name = [‘sharad’, ‘omkar’, ‘dev’,’ram’,’sham’]
place = [‘forest’,’zoo’ ,’hotel’,’farm’]
food = [‘chicken’,’panner’,’”dosa”’,’bhajibhakar’]
drinks= [‘water’,’juice’,’”milk”’]
print(random.choice(time)+ “ “+random.choice(person_name)+” went to “+ random.choice(place)+” and he eaten “+random.choice(food)
+ “ and also drank “+ random.choice(drinks)
#choice module use for randomly choice time from time list same like other
+ Sign use for joined
“ ” all those content under “” just string
Output :
yesterday omkar went to farm and he eaten panner and also drank water



