Exercise 31: Making a decision
In the upper part of the book you print something and call the function, but everything is done in a straight line. Your script starts from the top row and runs all the way to the end, but there is no pivot point to determine the flow of the program. Now that you've learned if, else, and elif , you can start creating scripts that contain conditional judgments.
In the previous script you wrote a series of simple quiz tests. In this section of the script, you will need to ask the user to make a decision based on the user's answer. Write down the script and tinker for a while to see how it works.
1 Print "You enter a dark, with a doors. Do you go through door #1 or door #2?"2 3Door = Raw_input (">")4 5 ifDoor = ="1":6 Print "there ' s a giant bear here eating a cheese cake. What does?"7 Print "1. Take the cake."8 Print "2. Scream at the bear."9 TenBear = Raw_input (">") One A ifBear = ="1": - Print "The bear eats your face off. Good job!" - elifBear = ="2": the Print "The bear eats your legs off. Good job!" - Else: - Print "Well , doing%s is probably better. Bear runs away."% Bear - + elifDoor = ="2": - Print "You stare to the endless Abyss at Cthulhu ' s retina." + Print "1. Blueberries." A Print "2. Yellow jacket clothespins." at Print "3. Understanding revolvers yelling melodies." - -Insanity = Raw_input (">") - - ifInsanity = ="1" orInsanity = ="2": - Print "Your body survives powered by a mind of Jello. Good job!" in Else: - Print "The insanity rots your eyes into a pool of muck. Good job!" to + Else: - Print "You stumble around and fall to a knife and die. Good job!"
View Code
The point here is that you can put a "if statement" inside the "if statement". This is a powerful feature that can be used to create nested (nested) decisions in which one branch will lead to the sub-branch of another branch.
You need to understand the notion that an if statement contains an if statement. Do a bonus exercise so you can be sure that you really understand them.
The results you should see
I'm playing a little adventure game and I'm not playing the right level:
Bonus points Exercise
Add new parts to the game and change the position of the player to make the decision. Expand the game to the best of your ability, but don't make the game too weird.
Exercise Exercises
Stupid way to learn Python (31)