DEV Community

Claire Lu
Claire Lu

Posted on • Updated on

Python Project-Story generator

This is a simple Python Project that creates a story generator. We will randomly fill phrases in the sentence template and generate three stories at a time.

Sentence template is "Scene", "who1" + "action" + "who2".

First step, I want to create four lists.

Alt Text

Second step, in order to randomly select items from the list, I will use built in library random. random.choice()

Alt Text

Third step, I will print out the sentence.

Alt Text

Last, we need to generate 3 stories at a time. Calling the same print statement three more time only gives the same stories three more times, so I am going to use a while loop.

Alt Text

Here is the sample result I got.

import random

stories_times = 0
while stories_times < 3:

    scene = random.choice(["In an abandoned amusement park", "At a human zoo of the aliens", "In a city on Mars", "In a dream",
                       "In a hospital fulled with zombies", " In the magical world of Happy Porter",
                       "In the desert of Planet X", "On the edge of a cliff", " In a magical kingdom"])

    who1 = random.choice(["a talking rabbit that can't lie", "a goblin who wants to be human",
                      "a women who remembers her past life", "a bounty hunter who is also a serial killer",
                      "a time traveler who love eat zombies",
                      "a young witch who accidentally turn herself into a giant pig forever", "an alcoholic detective",
                      "an escaped criminal and a dangerous mental patient", "A snake demon"])

    who2 = random.choice(["a cult leader", "an astronaut who lost his memory ", "a dog who wanted world domination",
                      "a young couple who ran into the path of a psychopath", "baby Yoda",
                      "a bored vampire", " a mad scientist ", "the last chinese emperor", "an evil princess"])

    action = random.choice(["fall in love with", "fights against", "hunt for gold with",
                        "befriend", "revenge the death of", "robbed", " ate ", "forced to kill", "made clothe out of"])

    print(scene + ", " + who1 + " " + action + " " + who2 + ".")

    stories_times = stories_times + 1

else:
    print("The end.")
Enter fullscreen mode Exit fullscreen mode

Here are some final results.

Alt Text

I hope you liked it.

Top comments (2)

Collapse
 
iceorfiresite profile image
Ice or Fire

Nice but please post the code in text instead of images.

Collapse
 
cxl3 profile image
Claire Lu

Great idea. I am going to fix this week:) Thank you so much.