python program error elif else if [closed] - python

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
def Game():
# Story Background
print "You decide to take a walk outside one night when you come accross a corn field."
print "You notice an omnious sound coming from the other side of the maze."
Enter = raw_input("Do you enter? (yes or no)")
if Enter == "Yes" or "yes":
print "You walk into the maze and the corn is so thick together you cant push through"
print "so you walk down the isle surrounded by corn and you come to an intersection."
turn = raw_input("Which way do you go? (Left, Right, Forward, Leave)")
if turn == "Left" or "left":
print "After you turn left you come accross a dead end and you are forced to turn around."
print "You return to the intersection."
turn2 = raw_input("Which way do you go? (Left, Forward, Leave)")
if turn2 == "Forward" or "forward":
print "you walk on deeper into the maze when you come to a left turn"
print "you turn left and come accross a crossroad."
turn3 = raw_input("Which way do you go? (Left, Right, Leave)")
if turn3 == "Right" or "right":
print "You come to a dead end and are forced to turn around"
turn4 = raw_input("Which way do you go? (Forward, Leave)")
if turn4 == "Forward" or "forward":
print "You walk to a hole in the ground stopping you from moving any further"
print "the hole seems to be filled with snakes so you cant go through it."
print "you are forced to leave the maze."
elif turn4 == "leave" or "Leave":
print "you leave the maze and return home."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn3 == "Left" or "left":
print "You walk to a hole in the ground stopping you from moving any further"
print "the hole seems to be filled with snakes so you cant go through it."
print "you are forced to leave the maze."
print "you leave the maze and return home."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn2 == "Left" or "left":
print "you turn Left into the maze when you come by a strange man laying on the ground."
man == raw_input("What do you do? (Help, keep going)")
if man == "Help" or "help":
print "you help the man up and he knocks you out cold"
print "you wake back up in your bed at home"
elif man == "keep going" or "Keep going":
print "You leave the man behing after stealing his wallet."
print "YOU HAVE REACHED THE END OF THE MAZE"
print "You realize the noise was the sound of a old farmer milking a cow."
print "The farmer nags at you for coming on private property."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn2 == "leave" or "Leave":
print "you leave the maze and return home."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn == "Forward" or "forward":
print "you move forward into the maze when you come by a strange man laying on the ground."
man == raw_input("What do you do? (Help, keep going)")
if man == "Help" or "help":
print "you help the man up and he knocks you out cold"
print "you wake back up in your bed at home"
elif man == "keep going" or "Keep going":
print "You leave the man behing after stealing his wallet."
print "YOU HAVE REACHED THE END OF THE MAZE"
print "You realize the noise was the sound of a old farmer milking a cow."
print "The farmer nags at you for coming on private property."
elif turn == "leave" or "Leave":
print "you leave the maze and return home."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn == "Right" or "right":
print "After you turn right you come into a left turn only path."
print "You turn left and you come to a crossroad."
turn = raw_input("Which way do you go? (Left, Right, Leave)")
if turn == "Right" or "right":
print "You come to a dead end and are forced to turn around"
turn = raw_input("Which way do you go? (Forward, Leave)")
if turn == "Forward" or "forward":
print "You walk to a hole in the ground stopping you from moving any further"
print "the hole seems to be filled with snakes so you cant go through it."
print "you are forced to leave the maze."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn == "Forward" or "forward":
print "You walk to a hole in the ground stopping you from moving any further"
print "the hole seems to be filled with snakes so you cant go through it."
print "you are forced to leave the maze."
else:
print "you walk into a wall and go into an irreversable coma"
elif turn == "Leave" or "leave":
print "you leave the maze and return home."
else:
print "you walk into a wall and go into an irreversable coma"
elif Enter == "No" or "no":
print "You walk on into the depths of the night and are robbed by a couple street thugs."
else:
print "you walk into a wall and go into an irreversable coma"
def main():
Game()
main()
when i use this program, no matter what i enter into the python shell, it says the same thing over and over again.. it wont take the raw_input statements into context and put them into the if statements

if Enter == "Yes" or "yes":
This is not how or works. This is interpreted as if (Enter == "Yes") or "yes":. In python, non-empty strings are always true, so all of the if statements like that will be true. I would suggest something like
if Enter.lower() == "yes":
which takes care of all of the upper/lower case combinations.

Your syntax is wrong here:
if Enter == "Yes" or "yes":
This condition will always be true. Enter == "Yes" is first evaluated. If boolean representation is False, the boolean representation of "yes" will be considered. bool("yes") is always True.
Consider doing something like:
if Enter in ('Yes', 'yes'):
Or:
if Enter.lower() == 'yes':

Related

Does Python basically execute code from the top to the bottom? [duplicate]

I am currently learning how to code in Python and I have stumbled across this code in the book I am learning from (Learning Python the Hard Way [I don't recommend to anyone that JUST started coding btw]).
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "Take honey":
dead("The bear looks at you then slaps you.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "Taunt Bear" and bear_moved:
dead("The bear gets pissed off and chews your legs off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ")
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why, "Good job!"
exit(0)
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
I have always thought that Python reads codes from left to right and from up to down but in the code above, it starts running the program from
def start():
print "You are in a dark room."
I don't understand what is making Python do this, if anyone can clear this up for me it would be of great help. Thanks a lot in advance.
I have always thought that Python reads codes from left to right and from up to down
Reads, yes, top-down, left to right.
The def gold_room(): only defines function gold_room, it does not run it. Without gold_room() somewhere below, it will never be executed. Same with start().

Prompting for numbers instead of letters Python 2.7.x [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
So I have a few questions about the code below.
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
gold_greedy = "50"
if next > gold_greedy:
dead("You are too greedy to live, die.")
elif next < gold_greedy:
dead("You are fair and therefore you win.")
else:
dead("Man, you BARELY made it")
def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "Take honey":
dead("The bear looks at you then slaps you.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "Taunt Bear" and bear_moved:
dead("The bear gets pissed off and chews your legs off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?"
next = raw_input("> ")
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why, "Good job!"
exit(0)
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
When I get to the gold_room, why when I take any letters instead of numbers it gives me "You are too greedy to live, die. Good job!", shouldn't it give me an error message? or give me the "Man, you BARELY made it" message?
If the user types anything other than whole numbers, how can I prompt him to type a number?
If you type:
print type(next)
you can see the next variable is of type str. You have to convert it to an integer by using the int() function:
next = int(raw_input("> "))

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

Invalid python syntax [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input(">")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "take honey":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed of and chews your legs off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head(flee/head)?"
next = raw_input("> ")
if next == "flee":
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why, "Good job!"
exit(0)
def dog_room():
print "you entered a room and you see a dog sleeping and the door behind you got locked by it self"
print "you see a sign that says this dog got a very good hearing sense, above a normals dog hearing sense."
print "and you see a spear beneath you."
print "and you can see that there is a bridge behind him."
print "what will you do now?try to go to the bridge, pick up the spear, try to sneak your way to the dog and hit him or attack the door."
print "(bridge/spear/sneak and hit/attack the door)"
spear = False
while True:
action = raw_input("Choose what you want to do")
if action == "bridge" and not spear:
death("the dog woke up rushed to you and ate you right after he ate your balls."
elif action == "sneak and attack" and not spear:
death("you sneaked your way to the dog, hit him, and the damage you made to him wasn't strong enough and he ate you right after he ate your ball.")
elif action == "spear" and not spear:
spear = True
print "you took the spear. what now?"
elif action == "spear" and spear
print "you already took the spear..."
elif action == "sneak and attack" and spear:
golden_room("you stabbed the dog and went across the safe bridge with no casualties and you managed to get to the golden room!!!!!!!!")
elif action == "attack the door":
print "you broke the wooden door, the dog woke up, rushed to you, you tried to escape but the dog was faster"
print "and ate you right after he ate your balls."
death()
else:
print "*face palm* come on learn how to type!"
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take(left/right/forward)?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
elif next = "forward":
dog_room()
else:
dead("You stumble around the room until you starve.")
start()
now for the problem
when i run it in power shell:
>>>python ex35.py
what i get is:
File "ex35.py", line 77
elif action == "sneak and attack" and not spear:
^
SyntaxError: invalid syntax
HELP!!! i tried to figure it out for an hour , hour and 30 minutes.
ty
if you are having problems to find the line
it is located right beneath the if line
which located inside the while loop which located inside the dog_room()
function.
Missing close paren:
death("the dog woke up rushed to you and ate you right after he ate your balls."
Missing trailing colon:
elif action == "spear" and spear
death("the dog woke up rushed to you and ate you right after he ate your balls."
--------------------------------------------------------------------------------^
You're missing a closing bracket )

Categories