【问题标题】:Python says variable is not defined, but it is...isn't it? [closed]Python说变量没有定义,但它是......不是吗? [关闭]
【发布时间】:2014-12-21 01:59:01
【问题描述】:

我是编程新手,目前正在学习 LPTHW。

我目前正在处理这个问题。但是,当我到达“客舱”时,终端告诉我“客舱”变量没有定义。这就是发生的事情。

代码如下:

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

现在,当我使用“路径”变量执行此操作时,这有效,那么这里有什么问题?我是否错过了一些明显而愚蠢的东西?

对不起,我是菜鸟。

【问题讨论】:

  • == 是一个比较运算符。 = 用于赋值。

标签: python variables nameerror defined


【解决方案1】:

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

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

cabin = int(raw_input("> "))

【讨论】:

  • 天哪,你是对的,谢谢 Ed 我会扇自己耳光
  • 没问题,很乐意提供帮助。请记得采纳答案! :)
  • 作为最近的前新手,请注意,如果你现在开始养成这种习惯,你会经常打自己的耳光。
猜你喜欢
  • 2015-07-02
  • 2016-08-11
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多