When I run the below code, I get indentation errors in many lines, such as line 6 and lines where I put exit(0):
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 a bear?"
bear_moved = False
while True:
next = raw_input("> ")
if next == "take honey":
dead("The bear looks at you and slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door and you can go now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea waht 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 dark room."
print "There is a door on 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 starved.")
start()
Don't mix tabs and spaces.
Use tools which will replace tabs with spaces (such as PyCharm).
By the way, you can use PyCharm to repair your broken indentation.
You are mixing tabs and spaces.
To fix your file on Linux :
tr \\t ' ' < file_in.py > file_out.py
And on Windows (7+) :
powershell -Command "(gc file_in.py) -replace '\t', ' ' | Out-File file_out.py"
Related
I was doing Excersice 35 ( Branches and Functions ) but the code I type in didn't work, after some time looking for mistakes, I tried to copy-paste from the book, but it didn't work
Here the code:
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 off and chews your leg 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 run it, it appears ">", that is the while-loop, but when I type "take honey", it says that dead isn't defined, I'm not understanding. ( Sorry if I wrote something wrong in english )
Indentation in python matters a lot, it looks like you did not indent the while statement, the whole thing under while including while itself should belong under def bear_room().
The bear_room should be like this or else you're running the while loop before the start() call:
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 off and chews your leg off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
Just move the function definition of dead to the top of the file (above where it's invoked).
from sys import exit
def dead(why):
print why, "Good job!"
exit(0)
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 off and chews your leg 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 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()
Most likely you have a whitespace error in your code. That means you forgot a tab some place and therefore your definition of dead (the function) is never reached. When it is called then (dead("...")) it's not defined and your script stops.
Here's how your code should look like:
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 off and chews your leg 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()
Really pay attention to the whitespaces!
I wanted to add more functionality to exercise 35 of Zed Shaw's LPTHW. The script runs without crashing and allows the player to revisit previous rooms, as I desired. However, in the bear_room the only way the player can get through is to scream at the bear, switching the bear_moved boolean to True.
If the player does this and then goes backward into the start room, it was my intent that the bear_moved boolean remained in the True position, meaning that the bear would still be moved away from the door upon re-entry.
That isn't happening when I run the script; Upon entering the bear_room for the first time I scream at the bear, causing it to move away from the door. I then back out of the room, returning to the start room. When I go back into the bear_room the bear has mysteriously plopped its fat self in front of the door again.
I placed the bear_moved boolean outside of the function just for this purpose--this was the only thing I could come up with to give that extra functionality to the program.
To recap, why doesn't the bear stay moved when I exit the bear_room and re-enter? How can I achieve the functionality I'm aiming for?
from sys import exit
bear_moved = False
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room(bear_moved)
else:
print "Type a number!"
gold_room()
def bear_room(bear_moved):
if bear_moved == False:
print "There's a bear in 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?"
elif bear_moved == True:
print "The bear has moved away from the door."
else:
pass
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False:
bear_moved = True
bear_room(bear_moved)
elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved == True:
gold_room()
elif 'open' in next and bear_moved == False:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room(bear_moved)
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("> ").lower()
if 'flee' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthuhlu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room(bear_moved)
elif 'right' in next:
cthulhu_room()
else:
start()
start()
Here's the top of your bear_room function, with the global functionality fixed. You still have to appropriately alter the calls to the routine.
You are still learning about Boolean values; note the changes I made in your tests. You don't have to test bear_moved == True; the variable itself is a True/False value. Comparing it against True does nothing. Comparing it against False is simply the not operation. I deleted your else: pass because the only way to reach that spot is if bear_moved is NaN (not a number), a value you shouldn't see in this program. Logically, you couldn't get to that clause at all.
Of course, there are still improvements to make in your program: fix the spelling and grammar errors, make the logic flow a little more cleanly (did you do any sort of flow chart for this?), and nest if statements to save work and reading time.
def bear_room():
global bear_moved
if not bear_moved:
print "There's a bear in 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?"
else:
print "The bear has moved away from the door."
I researched even more about Python's global and local variables. I now realize that I was changing a local variable within the function, not the global bear_moved. Which is exactly why the bear had plopped its fat self back in front of the door upon re-entry.
After stripping the parameter from the bear_room() function and making the bear_moved reference inside of it global, the function worked just as I had originally wanted it to. I also replaced the bear_moved == (boolean) expressions with simply bear_moved or not bear_moved.
from sys import exit
bear_moved = False
# Exercise 35 of Zed Shaw's LPTHW
# Player can now revisit previous rooms
# The bear_room function uses simple recursion
# instead of the while loop of the original script
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
if int(next) > 101:
dead("You greedy bastard!")
elif int(next) < 101:
print "Nice, you're not greedy! You win!"
exit(0)
else:
pass
elif 'back' in next or 'backtrack' in next:
bear_room()
else:
print "Type a number!"
gold_room()
def bear_room():
"""Globalizing bear_moved variable
and stripping parameter as advised."""
global bear_moved
if not bear_moved:
print "There's a bear in 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?"
elif bear_moved:
print "The bear has moved away from the door."
next = raw_input("> ")
if 'honey' in next:
dead("The looks at you and then slaps your face off.")
elif 'taunt' in next and not bear_moved:
bear_moved = True
bear_room()
elif 'taunt' in next and bear_moved:
dead("The bear flies into a rage and chews your leg off.")
elif 'open' in next and bear_moved:
gold_room()
elif 'open' in next and not bear_moved:
dead("The bear, still standing in the way, lunges up and rips your throat out.")
elif 'back' in next or 'backtrack' in next:
start()
else:
print "I got no idea what this means."
bear_room()
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("> ").lower()
if 'flee' in next or 'run' in next:
start()
elif 'head' in next:
dead("Well that was tasy!")
else:
cthulhu_room()
def dead(why):
print why, "Game Over!"
exit(0)
def start():
print "You're in a dark room."
print "You have no idea who you are or how you got here."
print "Your head is throbbing..."
print "There are two doors, one to your left, and one to your right."
print "Which door do you take?"
next = raw_input("> ").lower()
if 'left' in next:
bear_room()
elif 'right' in next:
cthulhu_room()
else:
start()
start()
I'm new to this website so please bear with my questioning problems on the website. I need some help to finish off this mini text-based game as homeowork of learning python the hard way book. This is the code I wrote. and I don't know the missing links and what I've done wrong. Help would be appreciated!
from sys import exit
def start():
print "You are in an old temple."
print "There is a door to your right and left or you can walk forwad."
print "Which one do you take?"
choice = raw_input("> ")
if choice == "left":
gold_room()
elif choice == "right":
trap_room()
elif choice == "forward":
monster_room()
else:
dead("you got caught by the ancient gods and you must be killed.")
start()
def monster_room():
print "you're in a room with a monster. what you gonna do?"
choice = raw_input("> ")
if "left" in choice:
print "you are going to the gold room"
gold_room()
elif "right" in choice:
print "you are going to the trap room"
trap_room()
else:
dead("couldnt understand what did you say so you are dead!")
def gold_room():
print "you chose the left room. now you are in a room with a pot of gold!"
print "you can take the pot."
print "or you can just rob the money in it."
print "or you go go to other rooms."
choice = raw_input("> ")
if choice == "take the pot":
print "you are a millionaire from now on!!!"
elif choice == "rob the money":
dead("you will never rest in piece!")
else choice == "another room":
monster_room()
def trap_room():
print "you are now in a trap room."
print "there is a hidden trap in this room."
print "be careful!"
print "you can go back to the monster room"
print "or you can find the trap"
choice = raw_input("> ")
if "find" in choice:
start()
elif "back" in choice:
gold_room()
def dead(why):
print why, "rekt!"
exit(0)
Ok, I've fixed it. Your code has several indentation mistakes, Python requires four spaces or one tab indentation after a def statement.
Another thing is, that you used else with a condition test (else choice == "another room":). That is wrong, it should be elif choice == "another room": or just else.
You also may have noticed, that I changed raw_input() to input(). This converts all inputs to strings (input() will interpret intergers as integers, lists as lists and so on...), and is also more secure.
The last thing is, you run your program (start()) before definign all called functions, this cannot work!
Your code (fixed):
from sys import exit
def start():
print("You are in an old temple.")
print("There is a door to your right and left or you can walk forwad.")
print("Which one do you take?")
choice = input("> ")
if choice == "left":
gold_room()
elif choice == "right":
trap_room()
elif choice == "forward":
monster_room()
else:
dead("you got caught by the ancient gods and you must be killed.")
def monster_room():
print("you're in a room with a monster. what you gonna do?")
choice = input("> ")
if "left" in choice:
print("you are going to the gold room")
gold_room()
elif "right" in choice:
print("you are going to the trap room")
trap_room()
else:
dead("couldn't understand what did you say so you are dead!")
def gold_room():
print("you chose the left room. now you are in a room with a pot of gold!")
print("you can take the pot.")
print("or you can just rob the money in it.")
print("or you go go to other rooms.")
choice = input("> ")
if choice == "take the pot":
print("you are a millionaire from now on!!!")
elif choice == "rob the money":
dead("you will never rest in piece!")
elif choice == "another room":
monster_room()
def trap_room():
print("you are now in a trap room.")
print("there is a hidden trap in this room.")
print("be careful!")
print("you can go back to the monster room")
print("or you can find the trap")
choice = input("> ")
if "find" in choice:
start()
elif "back" in choice:
gold_room()
def dead(why):
print(why, "rekt!")
exit(0)
start()
I would call the start() function at the end, after all the functions you defined. A usual way to write that is to write the following code at the end:
if __name__ == "__main__":
start()
This basically means that the programm runs the start() function at the end, if you're executing the file.
Furthermore you have to leave spaces after defining a function. You wrote:
def monster_room():
print "you're in a room with a monster. what you gonna do?"
but it should be:
def monster_room():
print "you're in a room with a monster. what you gonna do?"
If that didnt help, specify the problem
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The while loop in the dragon_room is not running and I'm not sure why. I get the '>>' prompt over and over again and the program never exits or brings me to another room.
from sys import exit
def food_room():
print "This room is full of candy bars. How many do you take?"
next = raw_input("gold room> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("type a number.")
if how_much < 5:
dead("You've starved, sorry")
else:
print "You've survived! congratulations."
exit(0)
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("bear room >")
if next == "take honey":
dead("Apparently the bear is pretty protective of his honey")
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 face off.")
elif next == "open door" and bear_moved:
food_room()
else:
"no idea what that means."
def dragon_room():
print "You've opened the door on a giant, fire-breathing dragon."
print "Do you flee or fight?"
dragon_moved = False
while True:
next = raw_input(">>")
if "flee" in next:
start()
elif next == "fight" and not dragon_moved:
print "You have slayed the dragon! There is a door behind him! you should open the door."
dragon_moved = True
elif next == "fight" and dragon_moved:
dead("killed")
elif next == "open door" and dragon_moved:
food_room()
else:
dead("you're not very good at following instructions.")
def dead(why):
print why, "good job!"
exit(0)
def start():
print "You are starving and 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":
dragon_room()
else:
dead("you starve.")
start()
It looks like you've got an indentation error:
while True:
next = raw_input(">>")
## the loop above runs forever -- you probably want to indent all of the
## below code to be inside the loop.
if "flee" in next:
start()
elif next == "fight" and not dragon_moved:
print "You have slayed the dragon! There is a door behind him! you should open the door."
dragon_moved = True
elif next == "fight" and dragon_moved:
dead("killed")
elif next == "open door" and dragon_moved:
food_room()
else:
dead("you're not very good at following instructions.")
You've indented improperly in dragon_room. Specifically:
while True:
next = raw_input(">>")
It will get a new next over and over forever, never running the rest.
Indent the rest of the function one more tab in:
while True:
next = raw_input(">>")
if "flee" in next:
start()
elif next == "fight" and not dragon_moved:
print "You have slayed the dragon! There is a door behind him! you should open the door."
dragon_moved = True
elif next == "fight" and dragon_moved:
dead("killed")
elif next == "open door" and dragon_moved:
food_room()
else:
dead("you're not very good at following instructions.")
It's because your indentation is off. Your conditionals are outside of the while loop so that the only thing within the while loop is the input statement.
I'm learning to code through learn python the hard way, and I've recently gotten stuck for the first time. For this exercise we're supposed to write our own game. I did so, but for some reason whenever I run it the right_room() function exits after I put in an answer, instead of proceeding to the next room. Any help would be greatly appreciated. Here's my code:
from sys import exit
def bear_room():
print "You are in a room with a bear."
print "You have two choices. left or right?"
next = raw_input("> ")
if next == "left":
left_room()
elif next == "right":
right_room()
else:
print "No idea what that means..."
def left_room():
print "You went left."
print "There are two doors. right or straight"
next = raw_input("> ")
if next == "right":
bear_room()
elif next == "straight":
second_left()
else:
print "What are you saying, bro?"
def second_left():
print "You went straight."
print "You again have two choices. straight or right?"
next = raw_input("> ")
if next == "straight":
print "You won! Congrats."
exit(0)
elif next == "right":
dead("You opened the door and walked off a cliff. Goodbye!")
else:
print "I didn't quite catch that."
def right_room():
print "You went right."
print "There are two doors. straight or right?"
next == raw_input("> ")
if next == "right":
dead("Oops, a tiger just ate you")
elif next == "straight":
second_right()
else:
"What?!?!?!"
def second_right():
print "You went straight"
print "Nice choice."
print "You have two choices: left or straight"
next == raw_input("> ")
if next == "left":
dead("You just fell 1 million feet to your death.")
elif next == "straight":
print "You made it out alive!"
exit(0)
else:
"WTF?"
def dead(reason):
print reason, "good job!"
exit(0)
def start():
print "You are about to enter a room."
bear_room()
start()
It looks like you're trying to assign to the next variable, but you used the equality check operator (==).