Python Story Generator Tutorial

Python Story Generator Tutorial

Table of Contents

  1. Introduction
  2. Setting Up the Project
  3. Gathering User Input
  4. Creating the Lists
  5. Randomly Selecting Phrases
  6. Constructing the Story
  7. Printing the Story
  8. Customizing the Story
  9. Running the Program
  10. Conclusion

Introduction

Welcome back to my YouTube Channel! Today's project is going to be a Story Generator using Python. Feel free to follow along and code with me. I would appreciate it so much if You liked, commented, and subscribed for more support. Without further ado, let's get into the video.

Setting Up the Project

I'm going to be using Python 3 for this project, and I'll be coding it on Atom, saving it on my Mac, then running it on the terminal. If you don't know how to do this, then watch my last video because I explain it all there.

Gathering User Input

First, I want to print "Hello reader, this is your story generator." You could write something of your choice, but just keep it on those lines. We can't run the code right now, but when we send it to the terminal, then we'll be able to see it there.

The next step is to make the code tell the audience what to write. To write their name in, We Are going to use an input function. So we're going to Type reader_name = input("Enter your name: "). When you run the code, this will ask for your name, then we'll input it into the reader_name variable, so then it'll be saved as your reader name.

We're now going to output the reader's name by printing "Hello" and then the name. So I'm going to write print("Hello " + reader_name).

Creating the Lists

Now let's Create the names, roles, quests, places, and weather of the story. We'll create a list for the code to randomly pick from in the story. In a list in Python, we use square brackets. Let me just make up some names:

names = ["Zara", "Liam"]

Remember to leave a space after and before the names, so then in the story it looks like there are spaces in between. We'll do the same now for places and quests.

places = ["London", "Paris"]

Okay, I've finished writing my places. If you can't think of any places of your own, then you can use this as an example. Now we're going to do the same method but for quests. Let's get started:

quests = ["Save the princess", "Find the lost treasure"]

The last list we need to make is for the weather. I promise this is the last one! For the weather, we can have a sunny day, rainy day, snowy day, etc. Let's just have:

weather = ["Sunny", "Rainy"]

Now we have all of our lists and options for different roles, names, places, quests, and weather. We're going to create a code which will randomly pick a phrase out of our lists, so it will help us generate a story.

Randomly Selecting Phrases

At the top of the code, we need to import the random function from Python. So we're going to write import random. This will let us make random choices.

We're going to have to choose a random word from each of our lists using the choice function. So we're going to type:

random_name = random.choice(names)
random_place = random.choice(places)
random_quest = random.choice(quests)
random_role = random.choice(roles)
random_weather = random.choice(weather)

Although this seems like a lot, the code will really just simply pick a random choice from each of the lists and put them into the story.

Constructing the Story

Now we're going to start writing out the story and inputting the choices. We're going to make a variable for the story and call that story. We're going to start off with "Once upon a time, a" then pick a role, called then the name. So let's write:

story = "Once upon a time, a " + random_role + " called " + random_name + "."

Add a space between that as well. Now, let's add:

story += " In a beautiful area called " + random_place + ", where it was " + random_weather + ", " + random_name + " will have to " + random_quest + "."

Okay, for the very last piece of this program, we'll have to make the print function to actually make our story appear on the screen. The first story you make might seem like nonsense or unusual. You can adjust the code and lists to make it fit in a certain theme. For example, if you want to make the story about winter, make all of your names, roles, quests, and places related to the frost or winter, something cold. We're just going to write print(story).

Customizing the Story

The story generator is now complete! You can play around with the lists and add more options to create your own unique stories. Feel free to experiment and customize the code to fit your needs.

Running the Program

To run the program, follow these steps:

  1. Open up your coding terminal.
  2. Navigate to the folder where the story_generator.py file is located.
  3. Type python3 story_generator.py and press Enter.

The program will ask for your name and then generate a random story Based on the choices from the lists.

Conclusion

Congratulations! You've successfully created a story generator using Python. I hope you enjoyed following along with this project and made your own story as exciting as possible. Remember to write them down in the comments and share your ideas. Don't forget to like, comment, and subscribe. Thank you again for watching and see you next time!

Highlights

  • Create a story generator using Python
  • Gather user input for customization
  • Use lists and random selection to generate phrases
  • Construct a story using the chosen phrases
  • Print the story on the screen
  • Customize the code and lists to fit different themes
  • Run the program in the terminal
  • Experiment and personalize your own stories

FAQ

  1. Can I use my own names, places, and quests in the story generator?

    • Absolutely! The lists provided are just examples. Feel free to customize them to your liking.
  2. How do I run the program on the terminal?

    • Open your coding terminal, navigate to the folder where the program is saved, and enter python3 story_generator.py.
  3. Can I add more phrases or categories to the story generator?

    • Certainly! You can expand the lists or create additional lists to enhance the variety of phrases in your story.
  4. How can I customize the generated story to fit a specific theme?

    • Modify the names, roles, quests, places, and weather options in the lists to Align with the desired theme. For example, if you want a winter-themed story, include names, quests, and places associated with winter.
  5. Can I share my generated stories with others?

    • Absolutely! Feel free to share your creations in the comments or with friends and family. Let your imagination run wild!

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content