Random story generator with Python -Programming tutorials EP011
Greetings;
Welcome to my blog. Its been a while i dropped another Python tutorial. The last one i shared here was about how to pip install any module that you might not have installed in your python. Probable reasons can be, due to you using an older version of python, or maybe a new module has just been released. So with pip install we can install them ourselves. Today i taught of showing you how to genrate a random story using python. I decied to make this story about this community and how the curators selecte and curate post. So without wasting much of your time lets head on to it.
Since we will be dealing with random stories, we will need to import the random module. This module can be found in almost any version of Python so no need to insatll it again. But if you happen to not have it, you can go ahead and install it.
To be able to create the story, we need to build it up by defining or creating three functions. Namely, Curators, Platforms, and Themes. So based on the story you want to write, you define these functions to suite them. Since i’m creating mine for this community these are the functions i need.
Now i can go ahead to define what each function holds such that in curators, names of the Diyhub curators like ["desco", "ashiksta", "stevenson", "tibfox", "semarekha"]. The platforms should also hold Hive blog and Diyhub and then last but not the least the themes they will be curating I decided to name a few ["coding","fashion","jewery","woodwork", "building", "decoration"]
Now its time to set up the story. Using def generate_story, we can instruct the computer to randomly select a curator, a platform and theme. So that we can get a variety of storys each time a person should run the code. The code required for this is random.choice(followed by the finction). We will do this for all three.
Now we can create the story we want. Somethig like a template. So we deifine the function story, Followed by how we want the story to appear. On my side i started with On one faithful day, {curator}. Now curator in the brackets means that a random name will be selected there. So for the full story template we have; On one faithfull day, {curator} was browsing {Platform} for {theme} post to curate on. He stumbled upon a creative and unique post by a subscriber, who had come up with a unique {adjective} {noun} that blew {curator} mind. After featuring the post on DIYHUB, it quickly went viral and inspired others to try out and then you return story.
In the template story, you might have seen nouns and adjectives. Yeah we need to break them down further or define them. Using the def generat_describtion(): we can go ahead to define the two functions nouns and adjectives.
And then with description can be selected at random by random.choice(nouns) and random.choice(adjectives). Then return story.
The code is all but complete untill you type print(generate_story) and run the code.
When we run the code, we get
The story goes “On a faithful day, stevenson was browsing HiveDIhub for coding post to curate on. He stumbled upon a creative and unique post by a subscriber, who had come up with a unique post that blew stevenson’s mind. After featuring the post on DIYHUB, it quickly went viral and inspired others to try out”