Lesson 15. Capstone project: choose your own adventure

 

After reading lesson 15, you’ll be able to

  • Write code for a choose-your-own-adventure program
  • Use branches to set up paths through the program

This capstone project is somewhat open-ended.

The problem

You’ll use conditionals and branching to create a story. At each scene, the user will enter a word. The word will tell the program which path to continue following. Your program should handle all possible paths that the user might choose, but doesn’t need to handle any unexpected input from the user.

The walk-through you’ll see is one of many possible others; be as creative as you want with your storyline!

15.1. Outlining the game rules

Anytime you’re getting input from users, you should be aware that they might not play by the rules. In your program, specify what you expect from them and warn them that anything else might make the program end.

A simple print statement will suffice, as in the following listing.

Listing 15.1. Getting user input
print("You are on a deserted island in a 2D world.")
print("Try to survive until rescue arrives!")
print("Available commands are in CAPITAL letters.")           #1
print("Any other command exits the program")                  #2
print("First LOOK around...")

According to the program rules, you’ll have to handle branches for any input in capital letters. There’s only one option at the start of the program, to help the user get accustomed to this input format.

15.2. Creating different paths

 
 
 
 

15.3. More choices? Yes, please!

 
 
 
 

Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage