Search code examples
pythonvariablesnameerrordefined

Python says variable is not defined, but it is...isn't it?


I'm an utter newbie to programming and I'm currently going through LPTHW.

I'm working on this at the moment. However, when I reached the "cabin" the terminal tells me that the "cabin" variable is not defined. This is what happens.

Here is the code:

print "You enter a dark room with two doors. Do you go through door #1 or door #2?"

door = raw_input("> ")

if door == "1":
    print "There's a giant bear here eating a cheese cake. What do you do?"
    print "1. Take the cake."
    print "2. Scream at the bear."

    bear = raw_input("> ")

    if bear == "1":
        print "The bear eats your face off. Good job!"
    elif bear == "2":
        print "The bear eats your legs off. Good job!"
    else:
        print "Well, doing %s is probably better. Bear runs away." % bear
        print "You have now entered a forest clearing. What do you do now?"
        print "1. Take the left fork of the path."
        print "2. Take the right fork of the path."
        print "3. Go straight into the forest itself, not on any path."

        path = int(raw_input("> "))

        if path == 1:
            print "There is a pack of wolves on the path. They chase you down and eat you."
        elif path == 2:
            print "A forest ranger catches you and kills you for your booty. Good job, trespasser!"
        else:
            print "You trek through the forest and find an abandoned log cabin. What do you do?"
            print "1. Look inside the cabin."
            print "2. Ignore it and continue."
            print "3. Set up camp for the night beside the cabin, have a quick look inside but sleep in your camp."

            cabin == int(raw_input("> "))

            if cabin == 1:
                print "You find some tinned food and a rifle with a full magazine of ammunition. What do you do now?"
                print "1. Stay the night at the cabin."
                print "2. Head out into the woods and continue trekking overnight."

                plan = raw_input("> ")

                if plan == "1":
                    print "The cabin burns down during the night and you die. Bad luck!"
                else:
                    print "You stumble around in the dark, fire all your ammunition off at shadows, and get eaten by wolves. Bad luck!"

            elif cabin == 2:
                print "You stumble through the forest in the dark and get eaten by a bear. Oh well."

            else:
                print "The cabin burns down overnight but you are fine. The next morning you find your way out of the forest and back to town. Well done!"

elif door == "2":
    print "You stare into the endless abyss at Cthulu's retina."
    print "1. Blueberries."
    print "2. Yellow jacket clothespins."
    print "3. Understanding revolvers yelling melodies."

    insanity = raw_input("> ")

    if insanity == "1" or insanity == "2":
        print "Your body survives powered by a mind of jelly. Good job!"
    else:
        print "The insanity rots your eyes into a pool of muck. Good job!"


else:
    print "You stumble around and fall on a knife and die. Good job!"

Powershell gives me this error:

Traceback (most recent call last):
    File "ex31.py", line 35, in (module)
        cabin == int(raw_input("> "))
NameError: name "cabin" is not defined

Now, this worked when I did it with the "path" variable, so what is the problem here? Am I missing something glaringly obvious and stupid?

Sorry for being a noob.


Solution

  • You're using the wrong operator. = sets the value, while == is the comparison operator for equality. Change

    cabin == int(raw_input("> "))
    

    to

    cabin = int(raw_input("> "))