Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python说变量没有定义,但它是;不是吗?_Python_Variables_Nameerror_Defined - Fatal编程技术网

Python说变量没有定义,但它是;不是吗?

Python说变量没有定义,但它是;不是吗?,python,variables,nameerror,defined,Python,Variables,Nameerror,Defined,我是一个完全的编程新手,目前正在进行LPTHW 我现在正在做这个。但是,当我到达“cabin”时,终端告诉我“cabin”变量没有定义。事情就是这样 代码如下: 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 eati

我是一个完全的编程新手,目前正在进行LPTHW

我现在正在做这个。但是,当我到达“cabin”时,终端告诉我“cabin”变量没有定义。事情就是这样

代码如下:

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向我提供了以下错误:

Traceback (most recent call last):
    File "ex31.py", line 35, in (module)
        cabin == int(raw_input("> "))
NameError: name "cabin" is not defined
现在,当我使用“path”变量时,它起作用了,那么这里的问题是什么?我是不是错过了一些明显而愚蠢的东西


对不起,我是个傻瓜。

您使用了错误的运算符
=
设置值,而
=
是相等的比较运算符。改变

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


你用错了接线员
=
设置值,而
=
是相等的比较运算符。改变

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


=
是一个比较运算符<代码>=用于赋值。
==
是比较运算符<代码>=用于分配任务。哦,你说得对,谢谢Ed。我会打自己一巴掌的。没问题,很乐意帮忙。请记住接受答案!:)作为一个最近的、以前的新手,请注意,如果你现在开始养成这种习惯,你会给自己很多耳光。哦,你说得对,谢谢你,埃德,我会给自己耳光的。没问题,我很乐意帮助你。请记住接受答案!:)作为一个最近的、以前的新手,请注意,如果你现在开始养成这种习惯,你会给自己很多耳光。