Write a program with a single line:

Run it, and type “dog” at the prompt.  You get an error, which makes sense since you tried to convert ‘dog’ to an integer.  But you’ve written a fragile program.  To fix it, do this instead:

This fixes the problem, and it makes sense.  If the ‘try’ block gives any errors, it stops and runs the except block.  You’ll use this in the assignments below:

The Beginning…

You’re going to make an adventure story, but first just make the beginning.  Make a single function, start( ), that begins your story.  Don’t have it call any other functions except itself.    Instead, just print some feedback for the user about what happens.  Call this beginning.py .

Global Variable

In general, if you make or change a variable in a function, those changes are NOT global.  Consider the following:

At first glance, it looks like running found_key() should make the value of key True for the whole program, but it does not.  The change of key to True in found_key() is a local change.  In fact, the key variable in the found_key( ) function is not the same variable as the key variable initially set to False.    Try adding a line at the beginning of the found_key() function, like this:

Now the key variable in found_key( ) is the same one used elsewhere.

Adventure Story:  Planning

Your job is to make a “Choose your own adventure” story, using Python and functions.  Start by planning out your story with Pages or Google Docs.  Each ‘node’ of your story should have choices for the user, unless it an ending to the story.  The story should be inside boxes, with choices as arrows.

 

Each box or ‘page’ will be a function in your program.

If you are going to use a global variable or two, indicate on the diagram how you will use them!  Some examples of global variables you could use are objects (like “key” or “water”), hunger (you could use a number), hit_points, or a piece of knowledge (password).

Save a pdf version of your plan as story_plan.pdf

Adventure Story

Write your adventure story!  You should have at least ten functions representing ‘pages’ in your story.  At least seven of them should have a choice for the user (input).   Bonus points for:

Fun use of a global variable or two

Appropriate use of randomness

Non-linear story (maybe use a parameter or global variable to modify a ‘page’)

Something else?  Have the user solve a puzzle by exploring your story.  You can be super creative if you focus on designing the story first, then on writing code to implement your vision!

Save your final story as story.py.  If you make more than one version, save them as story1.py, story2.py, etc.

 

 

Resources:

Coming Soon…