Question

I'm doing exercise 35 in the book Learn Python the Hard Way, and I can't seem to figure out this error. It's probably something silly but I can't seem to figure it out.

The error is on line 75, 69 and 26, modules start(), in start bear_room() and in bear_room next2 = raw_input("> ")

from sys import exit


def gold_room():
    print "This room is full of gold. How much do you take?"
    next1 = raw_input("< ")
    if "0" in next1 or "1" in next1:
        how_much = int(next1)
    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:
        next2 = raw_input("> ")
    if next2 == "take honey":
        dead("The bear looks at you then pimp slaps your face off.")
    elif next2 == "taunt bear" and not bear_moved:
        print "The bear has moved from the door. You can go through it now."
        bear_moved = True
    elif next2 == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your crotch off.")
    elif next2 == "open door" and bear_moved:
        gold_room()
    else:
        print "I got no idea what that means."


def cthulu_room():
    print "Here you see the great evil Cthulu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life of eat your head?"
    next3 = raw_input("> ")
    if "head" in next3:
        dead("Well that was tasty!")
    elif "flee" in next3:
        start()
    else:
        cthulu_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?"
    next4 = raw_input("> ")
    if next4 == "left":
        bear_room()
    elif next4 == "right":
        cthulu_room()
    else:
        dead("You stumble around the room until you starve.")


start()
Was it helpful?

Solution

I didn't hit your specific error, but it would have been hard to tell if I had based on your description. In the future, you should post your stack trace with line exceptions.

What I did find was that you had an infinite loop in your code. This part:

while True:
    next2 = raw_input("> ")
if next2 == "take honey":
# snip

Never would have exited. True will always be True, so it just keeps prompting you with the raw_input. By changing the rest of that function to look like this...

while True:
    next2 = raw_input("> ")
    if next2 == "take honey":
        dead("The bear looks at you then pimp slaps your face off.")
    elif next2 == "taunt bear" and not bear_moved:
        print "The bear has moved from the door. You can go through it now."
        bear_moved = True
    elif next2 == "taunt bear" and bear_moved:
        dead("The bear gets pissed off and chews your crotch off.")
    elif next2 == "open door" and bear_moved:
        gold_room()
    else:
        print "I got no idea what that means."

...we are telling Python to take the results of the prompt stored in next2 and evaluate the rest of the checks. This way, it will exit the function by calling other functions such as dead(), or gold_room(). This will exit the bear_room() function even though the while True condition has never exited. While loops are exited when the condition evaluates to False. Because True will never equal False, we have to exit some other way, such as calling another function like dead() which will terminate by calling exit().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top