How can I go back to previous code in Python? - python

I'm pretty new to programming, but I've got a quick question. I'm trying to write a sort of "choose your own adventure" game, but I've run into a problem. I'm only really as far into if statements in the code, but I want to be able to send the user back to previous code when they type something.
For example:
print "You are in a room with two doors to either side of you."
choiceOne = raw_input("Which way will you go?")
choiceOne = choiceOne.lower()
if choiceOne = "r" or choiceOne = "right":
print "You go through the right door and find yourself at a dead end."
elif choiceOne = "l" or choiceOne = "left":
print "You go through the left door and find yourself in a room with one more door."
else:
print "Please choose left or right."
In the if statement, I want to send the user back to choiceOne's raw_input(). In the elif statement, I want to give the user the option to either proceed through the next door, or return to the first room to see what secrets the other door may hold. Is there any way to do this? I don't care if the way is complicated or whatever, I just want to get this working.

Are you looking for a while loop?
I think that this website explains it very well: http://www.tutorialspoint.com/python/python_while_loop.htm
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
→
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

Use a while loop:
while True:
print "You are in a room with two doors to either side of you."
choice_one = raw_input("Which way will you go?").lower()
if choice_one == "r" or choice_one == "right":
print "You go through the right door and find yourself at a dead end."
continue # go back to choice_one
elif choice_one == "l" or choice_one == "left":
print "You go through the left door and find yourself in a room with one more door."
choice_two = raw_input("Enter 1 return the the first room or 2 to proceed to the next room")
if choice_two == "1":
# code go to first room
else:
# code go to next room
else:
print "Please choose left or right."
You need to use == for a comparison check, = is for assignment.
To break the loop you could add a print outside the loop print "Enter e to quit the game":
Then in your code add:
elif choice_one == "e":
print "Goodbye"
break

Related

Stuck at 36th exercise of Learn Python the Hard Way [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
This is the code I have written for Learn Python the Hard Way exercise 36. But I am not able to run the code in the door_1 function. If I choose 3 as an option and then left or right anything which is then stored in dir, the output is "lion ate you", no matter what I type.
from sys import exit #importing module from lib
def Tadaa():
print "This is a place where you will get a surprise"
next = int(raw_input("Enter any number from 1-10 :"))
if next <= 10:
if next % 2 == 0 :
print "You will be buying me a shake :)."
else :
print "You will be getting a shake by me."
else :
print "Do it correctly."
def door_1():
print "There are 3 doors . Choose any door from the the remaining three doors"
print "Lets hope for the best "
next = raw_input("Enter your option :")
if next == "1":
print "abc "
elif next == "2":
print "abc"
elif next == "3":
print "You have entered 3rd door ."
print "Here are 2 doors one on left and one on right."
dir = raw_input("Choose which door do you wnat to enter :")
if dir == "left" or "Left":
print "Lion ate you . You are dead ."
elif dir == "right" or "Right" :
print "You will be getting a surprise"
Tadaa()
else :
print "abc"
else :
print "abc"
def door_2():
print "There are two doors A and B which will decide your fate"
next = raw_input("Enter the door ")
if next == "A" or "a":
door_1()
elif next == "B" or "b":
print "You are back from where you have started"
start()
else :
print "I got no idea what that means."
exit(0)
def start():
print "You are in dark room"
print "There is a door to your right and left ."
print "Which one do you take"
next = raw_input("> ")
if next == "Right" or "right":
door_2()
elif next == "Left" or "left":
door_1()
else :
print "abc"
start()
The problem is your statement:
if dir=="left" or "Left":
What you want is
if dir=="left" or dir=="Left":
In effect, what just doing or "Left" is doing, is checking whether a string that you've just created exists or not. Put another way, its similar to:
x='Left'
if x:
X does exist, so if X is True.
The crux here is to always evaluate statements in quantum, and when you're using and in conjunction with or statements, make sure you use brackets to be explicit. if statement_one or statement_two.

If returning wrong answer (python)

I am working through the "Learn python the hardway." I apologize if this is a duplicate but I really can't figure out what I'm doing wrong. When I enter anything less than 50 it tells me to try again, and upon the second time calls a string in another function. If I enter something greater than 50 same thing on the second entry it calls another string in another function. it will call from green_dragon() and print "Both dragons cook you alive and eat you." Thank you for any insight you are able to offer. I apologize for the simplicity, lol. Had to make my own "game and I'm not that creative yet, lol.
def gold_room():
print "You have entered a large room filled with gold."
print "How many pieces of this gold are you going to try and take?"
choice = raw_input("> ")
how_much = int(choice)
too_much = False
while True:
if how_much <= 50 and too_much:
print "Nice! you're not too greedy!"
print "Enjoy the gold you lucky S.O.B!"
exit("Bye!")
elif how_much > 50 and not too_much:
print "You greedy MFKR!"
print "Angered by your greed,"
print "the dragons roar and scare you into taking less."
else:
print "Try again!"
return how_much
def green_dragon():
print "You approach the green dragon."
print "It looks at you warily."
print "What do you do?"
wrong_ans = False
while True:
choice = raw_input("> ")
if choice == "yell at dragon" and wrong_ans:
dead("The Green Dragon incinerates you with it's fire breath!")
elif choice == "approach slowly" and not wrong_ans:
print "It shows you its chained collar."
elif choice == "remove collar"and not wrong_ans:
print "The dragon thanks you by showing you into a new room."
gold_room()
else:
print "Both dragons cook you alive and eat you."
exit()
too_much = False
if <= 50 and too_much
if too_much is set to False, why do you expect the if expression to evaluate to true ? It will never go inside the if.
Move user input inside loop as well.
EDIT:
To stop your while loop:
too_much = True
while too_much:
choice = raw_input("> ")
how_much = int(choice)
if how_much <= 50:
print "Nice! you're not too greedy!"
print "Enjoy the gold you lucky S.O.B!"
too_much = False
else:
print "You greedy MFKR!"
print "Angered by your greed,"
print "the dragons roar and scare you into taking less."

Defining functions in python - code block wont run

I'm been having a lot of trouble properly using functions in python. Im a beginner so I'm creating this kinda maze like game, and want to use functions so that a user can return to previous locations if they choose to. The problem is that all the code after the "def sec2():" does not run, and it totally skips over this block of code. So if the user were to run the program and choose left, the program finishes with "Ah, nevermind, remember those numbers," never prompting the user with anything or printing anything from sec2. I believe my problems could be occurring with indentation. If anyone has any idea as to why the code under my functions isn't executing please let me know! Many thanks!
print ("********")
print ("Hey there soldier. This is General Chris.")
print ("I understand you are in quite the situation!")
print ("Just 4 hours ago, your patrol was ambushed by ISIS...You may not rememeber much after being knocked unconscious!")
name = input('Whats your name, soldier?')
print ("********")
print ("Alright, here's the deal",name)
print ("You are now being held hostage in an encampment near Soran, Iraq.")
print ("Unfortunately for you, our hackers have recieved intel that your captors plan on executing you in just two hours.")
print ("You dont have long to make your escape! Get moving fast!")
def sec1():
print ("********")
print ("You have two doors in front of you. Do you choose the door on the left or right?")
room1 = input('Type L or R and hit Enter.')
if room1 == "L":
print ("********")
print ("Good choice",name)
print ("Whats this? A slip of paper says '8642' on it...Could it mean something?")
print ("Ah, nevermind! Remember those numbers!")
def sec2():
print ("********")
print ("Now you have a choice between crawling into the cieling, or using the door!")
room2 = input('Type C for cieling or D for door, and hit Enter!')
if room2 == "C":
print ("********")
print ("Sure is dark up here in the ducts!")
print ("Stay quiet, and move very slowly.")
def ductoptionroom():
print ("Do you want to continue into the ducts or retreat?")
ductoption = input('Type C or R and hit Enter!')
if ductoption == "C":
print ("You are continuing into the ducts!")
elif ductoption == "R":
sec2()
else:
print ("Focus on the mission!")
elif room2 == "D":
print ("********")
print ("You slowly turn the door knob and see a guard standing there...He doesnt notice you!")
print ("Look, theres a piece of rope on the ground! You could use this to strangle the guard!")
def guardkillroom():
print ("Do you want to try and kill the guard so you can continue on your escape?")
guardkill = input ('Type K for kill or R for retreat and hit Enter!')
if guardkill == "K":
print ("********")
print ("You sneak behind the unsuspecting guard and quickly pull the rope over his neck!")
print ("You've strangled the guard! Now you can continue on your mission!")
elif guardkill == "R":
guardkillroom()
else:
print ("We dont have all day!")
guardkillroom()
else:
print ("Focus soldier!")
room2()
elif room1 == "R":
print ("********")
print ("Uh oh. Two guards are in this room. This seems dangerous.")
print ("Do you want to retreat or coninue?")
roomr = input('Type R or C and hit enter.')
if roomr == "R":
print ("********")
print ("Good choice!")
sec1()
elif roomr == "C":
print ("********")
print ("You continue and are spotted by a guard.")
print ("***MIISSION FAILED***")
def gameover1():
print ("Do you want to retry?")
retry1 = input("Type Y or N and hit enter!")
if retry1 == "Y":
sec1()
elif retry1 == "N":
print ("********")
print ("Thanks for playing!")
else:
gameover1()
sec1()
It looks like you only ever define sec2 and you never actually call it. When you do something like def myfunc() it just tells python what should happen when that function is called. The code will never actually be ran until you run myfunc() from somewhere else in the code. And the only place sec2 is called is recursively from within sec2 (if the player decides to retreat from the ducts)
So to use sec2 you need to call it from somewhere else within sec1
I'm not sure where that should be based upon a quick reading of the game but for testing you could do something like the following
print ("Ah, nevermind! Remember those numbers!")
def sec2():
....
elif room2 == "D":
sec2()
Additionally since sec2 is defined inside of sec1 that means sec2 can only ever be called inside of sec1 as well. I suspect that was not what was intended (though I could be wrong). To fix that you could do the following
def sec2():
...
def sec1():
... #All the same code as before except for the sec2 definition
sec1()

How do I move to different parts of code in this game/story

After the initial question of how much do you take, it works fine. If you type 0 you die, 5 million it says nice take. After it says nice take, it exits the program and forgets the rest of the code.
How do I get python to load the next part of the code and run it.
from sys import exit
def bank_vault():
print "This room is full of money up to 5 million dollars. How much do you take?"
choice = raw_input("> ")
if "0" in choice:
dead("You are robbing a bank and took nothing... The cops shot you in the face.")
how_much = int(choice)
if how_much < 5000000:
print "Nice take, now you have to escape!"
escape_route()
def escape_route():
print "There are cops blocking the front door."
print "There are cops blocking the back door."
print "There is no way to get out of there."
print "You see a small opening on the ceiling."
print "Type ceiling to go through it."
print "Or type stay to try your luck."
escaped_cops = False
while True:
choice = raw_input("> ")
if choice == "stay":
dead("Your bank robbing friends left your stupid ass and the cops shot you in the face. Idiot move dipshit.")
elif choice == "ceiling" and not escaped_cops:
print "You escaped! Now wash that money and don't go to prison."
escaped_cops = True
def dead(why):
print why, ""
exit(0)
bank_vault()
Cool game. I love games and would like to try to improve your code. Code below works on Python 3. It should work on Python 2:
from sys import exit
try: input = raw_input # To make it work both in Python 2 and 3
except NameError: pass
def bank_vault():
print("This room is full of money up to 5 million dollars. How much do you take?")
how_much = int(input("> ")) # Why not eliminate choice variable here?
if how_much == 0:
dead("You are robbing a bank and took nothing... The cops shot you in the face.")
elif how_much < 5000000: # Changed to else condition
print("Nice take, now you have to escape!")
else: # Added a check
dead("There isn't that much!")
def escape_route():
print("There are cops blocking the front door.")
print("There are cops blocking the back door.")
print("There is no way to get out of there.")
print("You see a small opening on the ceiling.")
print("Type ceiling to go through it.")
print("Or type stay to try your luck.")
escaped_cops = False
while not escaped_cops:
choice = input("> ")
if choice == "stay":
dead("Your bank robbing friends left your stupid ass and the cops shot you in the face. Idiot move dipshit.")
elif choice == "ceiling":
print("You escaped! Now wash that money and don't go to prison.")
escaped_cops = True
def dead(why):
print(why)
exit(0)
bank_vault()
escape_route()
You have to call the function escape_route rather than exit.
Also, when checking for choice you call dead no matter what.
In reply to your comment:
You need to check if it's 0 then call dead, if it's not 0 don't bother with the else, go directly to the if how_much < 5000000 and call escape_route
You need to try and convert the input to an int if it isn't 0!
And what happens if they take more than 5 mil?

NameError: global name 'has_dynamite' is not defined

I'm new to programming. I'm learning python from here. I am on lesson 36 which says to make a small game. This is the game:
from sys import exit
def you_won():
print """
CONGRATUFUCKINGLATIONS!!!
You won this really easy but kind of cool game.
This is my first game but not the last.
Thanks for playing.
Do you want to start over? Yes or No?
"""
next = raw_input("> ")
if next == "Yes" or "yes":
start()
elif next == "No" or "no":
exit(0)
else:
try_again()
def try_again(function_name):
print "That doesn't make sense, try again."
function_name()
def youre_dead():
print "You died, do you want to start over? Yes or No?"
next = raw_input("> ")
if next == "Yes" or "yes":
start()
elif next == "No" or "no":
exit(0)
else:
try_again()
def main_room():
print """
You enter a grand room with a throne in the back.
Standing right in front of you is the Qurupeco.
He snarls at you and you bare your teeth.
You can:
1 Attack Qurupeco
2 Taunt Qurupeco
3 Run Away
"""
if has_dynamite == True:
print "A Use Dynamite"
if has_sword == True:
print "B Use Sword"
if has_bow == True:
print "C Use Bow"
next = raw_input("> ")
if next == "1":
"You attack the Qurupeco but he stabs you and carves your heart out with a spoon."
youre_dead()
elif next == "2":
"You taunt the Qurupeco and he bites off your legs and lets you bleed to death."
youre_dead()
elif next == "3":
print "You try to run away but the Qurupeco is too fast. He runs you down and eats your entire body."
youre_dead()
elif next == "A" or "a":
print "You throw a stick of dynamite into his mouth and it blows him into tiny marshmallow size pieces."
you_won()
elif next == "B" or "b":
"You slice him squarely in two equal pieces."
you_won()
elif next == "C":
"You shoot him with your bow right between the eyes."
you_won()
def rightdoor_smallway():
print """
You go in the left door and see a lever at at the end of the room.
The flooring looks weird in this room.
You can:
1 Pull Lever
2 Leave
"""
next = raw_input("> ")
if next == "1":
print """
You pull the lever and feel the floor rush from beneath your feet.
You fall for a while and land head first on a giant spike.
"""
youre_dead()
if next == "2":
main_room()
def open_bow_chest():
print "You open the chest and find a bow and some arrows."
print "You take them and leave."
has_bow = True
main_room()
return has_bow
def leftdoor_smallway():
print """
You open the door to your left and see a chest.
You can:
1 Open Chest
2 Leave
"""
next = raw_input("> ")
if next == "1":
open_bow_chest()
elif next == "2":
main_room()
else:
try_again()
def forward_threeway():
print """
You walk forward for a while until you see two doors.
There is one to your right and one to your left.
You can:
1 Go Right
2 Go Left
3 Go Forward
Note: If you go left, you can't go right.
"""
next = raw_input("> ")
if next == "1":
rightdoor_smallway()
elif next == "2":
leftdoor_smallway()
elif next == "3":
main_room()
def three_way2():
forward_threeway()
def reason_meanman():
print """
You try to get the man to leave the damsel alone but he refuses.
When you insist he gets mad and snaps your neck.
"""
youre_dead()
def flirt_damsel():
print """
You flirt with the damsel, one thing leads to another and you end up having sex.
It was great.
Afterwards you tell her to wait for you there and you leave to finish your mission.
"""
three_way2()
def free_damsel():
print "You unchain the damsel, tell her where your king's castle is, and then leave."
three_way2()
def punch_meanman():
print """
You punch the mean man and he falls through a large random spike.
Ouch.
The damsel says thank you and tries her best to be seductive.
You can:
1 Flirt With Her
2 Free Her
3 Leave Her There
"""
next = raw_input("> ")
if next == "1":
flirt_damsel()
elif next == "2":
free_damsel()
elif next == "3":
three_way2()
else:
try_again()
def left_threeway():
print """
You go left for what seems like forever and finally see a door on the right.
You enter the room and see a mean looking man terrorizing a damsel that is chained to a post.
You can:
1 Reason With Mean Man
2 Punch Mean Man
3 Run Away
"""
next = raw_input("> ")
if next == "1":
reason_meanman()
elif next == "2":
punch_meanman()
elif next == "3":
three_way2()
else:
try_again()
def attack_gorilla():
print "You run at the gorilla and he bites your head off."
youre_dead()
def taunt_gorilla():
print """
You taunt the gorilla and he runs at you.
You trip him and he smacks his head into the wall so hard that he dies.
You can:
1 Leave
2 Open Chest
"""
next = raw_input("> ")
if next == "1":
three_way2()
elif next == "2":
print "You open the chest, find a sword, take it, and leave."
has_sword = True
three_way2()
else:
try_again()
return has_sword
def right_threeway():
print """
You go right for what seems like forever and finally find a door.
You enter the room and see a giant gorilla gaurding a chest.
You can:
1 Attack Gorilla
2 Taunt Gorilla
3 Run Away
"""
next = raw_input("> ")
if next == "1":
attack_gorilla()
elif next == "2":
taunt_gorilla()
elif next == "3":
three_way2()
else:
try_again()
def back_threeway():
print """
You walk outside and hear a click sound.
You try to open the door but it is now locked.
You freeze to death.
"""
youre_dead()
def three_way1():
print """
You enter the castle and see long hallways in every direction but behind you.
You can:
1 Go Forward
2 Go Left
3 Go Right
4 Go Back
Note: You can only pick one of the last three.
"""
next = raw_input("> ")
if next == "1":
forward_threeway()
elif next == "2":
left_threeway()
elif next == "3":
right_threeway()
elif next == "4":
back_threeway()
else:
try_again()
def go_inside():
print "You go back to the entrance, open the door, and go inside."
three_way1()
def poison_chest():
print "You struggle with the chest for a minute and finally figure out the mechanism, but, unfortunately, a poisonous gas is released and you die in agony."
youre_dead()
def outside_left():
print """
You go to the right and see a treasure chest three meters forward.
You can:
1 Go Inside
2 Open Chest
"""
next = raw_input("> ")
if next == "1":
turnback_outside()
elif next == "2":
poison_chest()
else:
try_again(outside_left)
def dynamite_inside():
print "You take the dynamite, it may be useful."
has_dynamite = True
go_inside()
return has_dynamite
def outside_right():
print """
You go left and see a stick of dynamite laying on the ground.
You can:
1 Go inside
2 Take Dynamite
"""
next = raw_input("> ")
if next == "1":
go_inside()
elif next == "2":
dynamite_inside()
else:
try_again(outside_right)
def start():
print """
You are a knight in shining armor, you lost your sword on the way here.
You have come to destroy the evil Qurupeco.
You are at the cold entrance to the Qurupeco's castle.
To the right of the entrance is a sign that reads: "Enter at your own risk!"
The walls are made of extremely tough stone.
You can:
1 Enter Castle
2 Go Left
3 Go Right
What do you do?
Note: You can't go left and right.
Note: Enter numbers when you're asked what you want to do.
"""
has_dynamite = False
has_sword = False
has_bow = False
next = raw_input("> ")
if next == "1":
three_way1()
elif next == "2":
outside_left()
elif next == "3":
outside_right()
else:
try_again(start)
return has_dynamite, has_sword, has_bow
start()
This is me playing the game and getting an error that I cant figure out.
jacob#HP-DX2450:~/Documents$ python ex36.py
You are a knight in shining armor, you lost your sword on the way here.
You have come to destroy the evil Qurupeco.
You are at the cold entrance to the Qurupeco's castle.
To the right of the entrance is a sign that reads: "Enter at your own risk!"
The walls are made of extremely tough stone.
You can:
1 Enter Castle
2 Go Left
3 Go Right
What do you do?
Note: You can't go left and right.
Note: Enter numbers when you're asked what you want to do.
> 3
You go left and see a stick of dynamite laying on the ground.
You can:
1 Go inside
2 Take Dynamite
> 2
You take the dynamite, it may be useful.
You go back to the entrance, open the door, and go inside.
You enter the castle and see long hallways in every direction but behind you.
You can:
1 Go Forward
2 Go Left
3 Go Right
4 Go Back
Note: You can only pick one of the last three.
> 1
You walk forward for a while until you see two doors.
There is one to your right and one to your left.
You can:
1 Go Right
2 Go Left
3 Go Forward
Note: If you go left, you can't go right.
> 3
You enter a grand room with a throne in the back.
Standing right in front of you is the Qurupeco.
He snarls at you and you bare your teeth.
You can:
1 Attack Qurupeco
2 Taunt Qurupeco
3 Run Away
Traceback (most recent call last):
File "ex36.py", line 368, in <module>
start()
File "ex36.py", line 362, in start
outside_right()
File "ex36.py", line 331, in outside_right
dynamite_inside()
File "ex36.py", line 316, in dynamite_inside
go_inside()
File "ex36.py", line 290, in go_inside
three_way1()
File "ex36.py", line 278, in three_way1
forward_threeway()
File "ex36.py", line 140, in forward_threeway
main_room()
File "ex36.py", line 47, in main_room
if has_dynamite == True:
NameError: global name 'has_dynamite' is not defined
jacob#HP-DX2450:~/Documents$
Any ideas?
The problem is arising due to
variable scope
You can check this question as well to clear your doubt regarding global or local scope:-
Python global/local variables
The variable
has_dynamite
is defined only inside the
start()
function. Therefore, it has only a local scope, to make it global define has_dynamite outside any functions and write your functions like this
def start():
global has_dynamite
Do this for all of the functions which use has_dynamite variable.
You can also pass the has_dynamite variable as a parameter to the function. ie instead of typing the above mentioned code, you can type
start(has_dynamite)
while calling the function, but be sure you include this parameter in the function definition also, otherwise it will show you an error
Here is the corrected code:-
from sys import exit
has_dynamite = True
has_sword = True
has_bow = True
def you_won():
print """
CONGRATUFUCKINGLATIONS!!!
You won this really easy but kind of cool game.
This is my first game but not the last.
Thanks for playing.
Do you want to start over? Yes or No?
"""
next = raw_input("> ")
if next == "Yes" or "yes":
start()
elif next == "No" or "no":
exit(0)
else:
try_again()
def try_again(function_name):
print "That doesn't make sense, try again."
function_name()
def youre_dead():
print "You died, do you want to start over? Yes or No?"
next = raw_input("> ")
if next == "Yes" or "yes":
start()
elif next == "No" or "no":
exit(0)
else:
try_again()
def main_room():
global has_dynamite
global has_sword
global has_bow
print """
You enter a grand room with a throne in the back.
Standing right in front of you is the Qurupeco.
He snarls at you and you bare your teeth.
You can:
1 Attack Qurupeco
2 Taunt Qurupeco
3 Run Away
"""
if has_dynamite == True:
print "A Use Dynamite"
if has_sword == True:
print "B Use Sword"
if has_bow == True:
print "C Use Bow"
next = raw_input("> ")
if next == "1":
"You attack the Qurupeco but he stabs you and carves your heart out with a spoon."
youre_dead()
elif next == "2":
"You taunt the Qurupeco and he bites off your legs and lets you bleed to death."
youre_dead()
elif next == "3":
print "You try to run away but the Qurupeco is too fast. He runs you down and eats your entire body."
youre_dead()
elif next == "A" or "a":
print "You throw a stick of dynamite into his mouth and it blows him into tiny marshmallow size pieces."
you_won()
elif next == "B" or "b":
"You slice him squarely in two equal pieces."
you_won()
elif next == "C":
"You shoot him with your bow right between the eyes."
you_won()
def rightdoor_smallway():
print """
You go in the left door and see a lever at at the end of the room.
The flooring looks weird in this room.
You can:
1 Pull Lever
2 Leave
"""
next = raw_input("> ")
if next == "1":
print """
You pull the lever and feel the floor rush from beneath your feet.
You fall for a while and land head first on a giant spike.
"""
youre_dead()
if next == "2":
main_room()
def open_bow_chest():
global has_bow
print "You open the chest and find a bow and some arrows."
print "You take them and leave."
has_bow = True
main_room()
return has_bow
def leftdoor_smallway():
print """
You open the door to your left and see a chest.
You can:
1 Open Chest
2 Leave
"""
next = raw_input("> ")
if next == "1":
open_bow_chest()
elif next == "2":
main_room()
else:
try_again()
def forward_threeway():
print """
You walk forward for a while until you see two doors.
There is one to your right and one to your left.
You can:
1 Go Right
2 Go Left
3 Go Forward
Note: If you go left, you can't go right.
"""
next = raw_input("> ")
if next == "1":
rightdoor_smallway()
elif next == "2":
leftdoor_smallway()
elif next == "3":
main_room()
def three_way2():
forward_threeway()
def reason_meanman():
print """
You try to get the man to leave the damsel alone but he refuses.
When you insist he gets mad and snaps your neck.
"""
youre_dead()
def flirt_damsel():
print """
You flirt with the damsel, one thing leads to another and you end up having sex.
It was great.
Afterwards you tell her to wait for you there and you leave to finish your mission.
"""
three_way2()
def free_damsel():
print "You unchain the damsel, tell her where your king's castle is, and then leave."
three_way2()
def punch_meanman():
print """
You punch the mean man and he falls through a large random spike.
Ouch.
The damsel says thank you and tries her best to be seductive.
You can:
1 Flirt With Her
2 Free Her
3 Leave Her There
"""
next = raw_input("> ")
if next == "1":
flirt_damsel()
elif next == "2":
free_damsel()
elif next == "3":
three_way2()
else:
try_again()
def left_threeway():
print """
You go left for what seems like forever and finally see a door on the right.
You enter the room and see a mean looking man terrorizing a damsel that is chained to a post.
You can:
1 Reason With Mean Man
2 Punch Mean Man
3 Run Away
"""
next = raw_input("> ")
if next == "1":
reason_meanman()
elif next == "2":
punch_meanman()
elif next == "3":
three_way2()
else:
try_again()
def attack_gorilla():
print "You run at the gorilla and he bites your head off."
youre_dead()
def taunt_gorilla():
print """
You taunt the gorilla and he runs at you.
You trip him and he smacks his head into the wall so hard that he dies.
You can:
1 Leave
2 Open Chest
"""
next = raw_input("> ")
if next == "1":
three_way2()
elif next == "2":
print "You open the chest, find a sword, take it, and leave."
has_sword = True
three_way2()
else:
try_again()
return has_sword
def right_threeway():
print """
You go right for what seems like forever and finally find a door.
You enter the room and see a giant gorilla gaurding a chest.
You can:
1 Attack Gorilla
2 Taunt Gorilla
3 Run Away
"""
next = raw_input("> ")
if next == "1":
attack_gorilla()
elif next == "2":
taunt_gorilla()
elif next == "3":
three_way2()
else:
try_again()
def back_threeway():
print """
You walk outside and hear a click sound.
You try to open the door but it is now locked.
You freeze to death.
"""
youre_dead()
def three_way1():
print """
You enter the castle and see long hallways in every direction but behind you.
You can:
1 Go Forward
2 Go Left
3 Go Right
4 Go Back
Note: You can only pick one of the last three.
"""
next = raw_input("> ")
if next == "1":
forward_threeway()
elif next == "2":
left_threeway()
elif next == "3":
right_threeway()
elif next == "4":
back_threeway()
else:
try_again()
def go_inside():
print "You go back to the entrance, open the door, and go inside."
three_way1()
def poison_chest():
print "You struggle with the chest for a minute and finally figure out the mechanism, but, unfortunately, a poisonous gas is released and you die in agony."
youre_dead()
def outside_left():
print """
You go to the right and see a treasure chest three meters forward.
You can:
1 Go Inside
2 Open Chest
"""
next = raw_input("> ")
if next == "1":
turnback_outside()
elif next == "2":
poison_chest()
else:
try_again(outside_left)
def dynamite_inside():
global has_dynamite
print "You take the dynamite, it may be useful."
has_dynamite = True
go_inside()
return has_dynamite
def outside_right():
print """
You go left and see a stick of dynamite laying on the ground.
You can:
1 Go inside
2 Take Dynamite
"""
next = raw_input("> ")
if next == "1":
go_inside()
elif next == "2":
dynamite_inside()
else:
try_again(outside_right)
def start():
global has_dynamite
global has_sword
global has_bow
print """
You are a knight in shining armor, you lost your sword on the way here.
You have come to destroy the evil Qurupeco.
You are at the cold entrance to the Qurupeco's castle.
To the right of the entrance is a sign that reads: "Enter at your own risk!"
The walls are made of extremely tough stone.
You can:
1 Enter Castle
2 Go Left
3 Go Right
What do you do?
Note: You can't go left and right.
Note: Enter numbers when you're asked what you want to do.
"""
has_dynamite = False
has_sword = False
has_bow = False
next = raw_input("> ")
if next == "1":
three_way1()
elif next == "2":
outside_left()
elif next == "3":
outside_right()
else:
try_again(start)
return has_dynamite, has_sword, has_bow
start()
I hope this helps
has_dynamite is not global but local variable. It exists only in start()
Put it outside all functions to make it global.
has_dynamite = False
has_sword = False
has_bow = False
start()
Now you can get value of has_dynamite in any function
But if you need to change global value you have to use global keyword
def dynamite_inside():
global has_dynamite
print "You take the dynamite, it may be useful."
has_dynamite = True
go_inside()
# return has_dynamite # unusable line

Categories