"A Story of the 3 Little Piggies"
If you aren’t familiar with “Choose your own Adventure” storybooks, they present the reader with the first page of the story. At the end of the page, there is a message that asks the reader what they want the main character to do. Each choice refers the reader to a certain page in the book to continue the story. In this lab, we will construct our own “Choose your own Adventure” stories!
Getting Started
Our program is going to start with a title along with the first paragraph of the story. To begin the program, we will make a call to the ‘main’ function:
You can see some student examples here.
1. def main(): 2. print("A Story about 3 Pigs: A Choose your own Adventure!") 3. 4. main()
In the block of code above, the interpreter will begin execution of the program at line 4 which is a function call to ‘main’. Following the execution of line 4, the interpreter will jump to the function ‘main’ and execute the block of code in the definition. Copy the code above and paste it into an ‘activecode’ window. By running the code, we can see that the title of the story is printed to the user. Now let’s take some time to change the title of the story to one that you want to write about. Now that we have a title printed out, we need to write the first paragraph of our story. Take a look at the following code:
def main(): print("A Story about 3 Pigs: A Choose your own Adventure!") print("\nIn the woods, there lived 3 little piggies: Pinky, Oink and Bacon. Pinky was a lazy pig \n" "and always liked to swing in his hammock while chewing on a long piece of straw. Oink was a \n" "outdoorsman and his favorite thing to do was go on long hunts for rare species of mushrooms.\n" "Bacon was an inquisitive little pig and couldn't get his head out of a book.\n") main()You can see that there is now another print command that prints out the first paragraph of our story. What might surprise you is the symbol ‘\n’. This symbol forces the program to start a newline. Without these symbols, the story would be on one long line. Try taking the ‘\n’ symbols out and run your code. Do you see the reason I placed them where I did?
After adding the above code to your program and running it in ‘activecode’, you should see that we now have a title and the first paragraph of our story printed to the user.
Now Try
Decisions, Decisions, Decisions
What we need now is for the program to ask the user to make a choice that will affect the direction of the story. We can do this by using an ‘input’ command. Take a look at the following code:
print("\n\nWhich piggy would you like to follow?") print("1. Pinky 2. Oink 3. Bacon") choice = input("Make your choice: ")
You can see that the following code asks the user to make a choice and receives the input from the user. Let’s add this to our code. Be sure to place it in the ‘main’ function after the first paragraph of your story.
Now Try
print("\n\n1. I want to follow Pinky.") print("2. I want to see what Oink does.”) print("3. I’m sure Bacon will do something interesting.”) choice = input("Make your choice: ")
Defining our Functions
You ask the user to make a choice, but now you have to change the story based on the user’s choice. Let’s create a set of functions that will output different versions of the story depending on what option the user chose. We can start by creating a function definition for Pinky. Remember to place the function definition above the ‘main’ function:
def pinky():
We have started to write our function definition, but we still need a block of code that will run when the ‘pinky’ function is called. Let’s add that now:
def pinky(): print("\nAfter spending one too many afternoons lazily swinging in the hammock, Pinky's parents \n" "kicked him out of the house and he off he went with all he owned in a red sack hanging on the\n" "end of a stick. Slinging his stick over his shoulder, Pinky began whistling his favorite\n" "tune. Soon Pinky ran into his friends Oink and Bacon. They were both sweating in the hot \n" "sun building themselves a shelter...\n")
Try running our code in an ‘activecode’ window. You can see that if you choose ‘1’, you will get the next part of the story from Pinky’s perspective printed out.
Now Try
Deciding When to Call our Function
Great job, we have our function definition written, but we need to call it in our ‘main’ function. We do this by adding a function call, but remember that we only want to call the ‘pinky’ function when the user chooses to follow Pinky the Pig. We can get our program to make such a decision by using a conditional statement. Try the following unary conditional statement in your ‘main’ function after the user inputs their choice:
#User chooses Pinky if(choice == '1'): pinky()Looking at this unary conditional statement, we can see that this line of code calls the ‘pinky’ function when the user chooses ‘1’, but what about when the user chooses ‘2’? Let’s add to the unary conditional statement to make it a binary selection.
What we really want is for the branch to be a chained conditional statement so we can account for if the user choose 1, 2 or 3. Let’s do that now and add to our binary conditional statement. If you have forgotten, the following is the general form of a chained conditional statement:
if( -Boolean expression- ): # Block of code 1 elif( -Boolean expression- ): # Block of code 2 else: # Block of code 3Looking at the code above, we can see that the chained conditional statement will execute block 1 when the first Boolean expression is true. If the first Boolean statement is false, the conditional statement will check the second Boolean expression. If it is true, the second block of code will be executed. If the second Boolean statement is false, then the 3rd block of code will always be executed. Remember that we can have as many ‘elif’ statements as we want in a chained conditional.
By now your ‘main’ function should have a chained conditional statement that calls 3 different functions based on the user’s input.
Now Try
Next Stage of the Story
We should have a program that allows the user to choose the second stage of the story. Your program should print out that second stage, but we still need to give the user choices on what the next step will be and receive their input. Do that now. After completing that, our program should look like the following:
def pinky(): print("\nAfter spending one too many afternoons lazily swinging in the hammock, Pinky's parents \n" "kicked him out of the house and he off he went with all he owned in a red sack hanging on the\n" "end of a stick. Slinging his stick over his shoulder, Pinky began whistling his favorite\n" "tune. Soon Pinky ran into his friends Oink and Bacon. They were both sweating in the hot \n" "sun building themselves a shelter...\n") print("What would you like to say to your friends?") print("1. Oink! Oink! 2. Got any food? 3. Can I stay with one of you guys?") question = input("Choose 1, 2 or 3: ")
Completing Your Adventure
Great, job! Now you need to get your program to make another decision based on the user’s input. Here I would include another chained conditional statement inside the function ‘pinky’ based on the user’s input. We need to continue this entire process for each stage of our story.
Project
Now Try
- Create a story that has at least 4 stages, at least 4 sentences per stage and at least 2 conditional statements per stage.
Extra Credit
Now Try
- Create a game that needs to be won.
- Add a graphical interface using Turtle.
Add-Ons
Take a look at the videos below to get a blueprint on how to add additional elements to your project.
Clicking The Screen
Typing Effect
Ripping Sound
Choosing Any Color
Adding Images
Use Keys to Move Turtle
Examples
Take a look below to see examples of the project in an outstanding range.
Find Your Brother
Flamingo
Little Red Rabbit Hood
Bomb Escape
Crimson Valley