Python:为何出现无明显原因的NameError?

1 投票
4 回答
558 浏览
提问于 2025-04-16 07:52
 from random import random

 def computer():
 computer = 0
 while computer < 21:
  val = int(random()*10)+1
  if (computer + val) > 21:
   break
  else:
   computer += val
 print "Ich habe ", computer, "!"
 return computer


    def player():
 player = 0
 loss = 0
 while player < 21:
                hval = int(random()*10)+1
                print "Du hast ", hval, "...\nNoch eine mit y..."
                if raw_input == "y":
                        player += hval
                        if player > 21:
                                print "Du hast verloren!"
                                loss = 1
                                break
                        else:
                                continue
                else:
                        player += hval
                        break
        return player
        return loss

    if __name__ == "__main__":
        player()
        if loss == 1: #This is where I get the NameError.
                pass
        else:
                computer()
                if computer > player:
                        print "Ich habe gewonnen!"
                else:
                        print "Du hast gewonnen"

我在player()这个函数里返回了损失值,但我不知道为什么我一直收到这个NameError错误。

4 个回答

0

在你的脚本中没有定义loss这个变量,建议你把整个脚本发出来,这样我们才能更好地理解你的代码。可能是你在player()函数里面用过的一个变量,但在其他地方没有定义。

1

当你写 return loss 的时候,它返回的是 loss 的值,而不是这个变量本身。因此,名字 loss 在调用的地方是不存在的。

你需要把结果赋值给一个本地变量(这个变量也可以叫做 loss),可以这样做:

loss = player()
3

让我们来整理一下这个乱七八糟的代码,看看有哪些错误:

from random import random # tip, check out randint (the function in the random module)

def computer():
    computer = 0
    while computer < 21:
        val = int(random()*10)+1 # please use some whitespace around operators

        if (computer + val) > 21:
            break

        else:
            computer += val

    print "Ich habe ", computer, "!"
    return computer

def player():
    player = 0
    loss = 0

    while player < 21:
        hval = int(random()*10)+1 # again, whitespace improves readability
        print "Du hast ", hval, "...\nNoch eine mit y..."

        if raw_input == "y": # raw_input is a function (hint you need to call the function)
                             # you're comparing the object against a string
                             # that will always yield false, therefore the player
                             # never gets the chance to enter another number

            # this never gets executed
            player += hval
            if player > 21:
                print "Du hast verloren!"
                loss = 1
                break

            else:
                continue

        # always gets executed
        else:
            player += hval
            break

    return player # this returns the value of player
    return loss # never reached, dead code, the return above has already left the function

if __name__ == "__main__":
    player() # returns the integer, but doesn't assign it to any name

    if loss == 1: # loss is not defined in this scope
        pass

    else:
        computer() # again, this doesn't assign the returned value

    if computer > player: # if this would get reached, it would fail yet again
                          # no name error this time, but you're comparing the function object
                          # against the player object, this will (at least in CPython)
                          # compare the memory addresses

        print "Ich habe gewonnen!"

    else:
        print "Du hast gewonnen"

现在你知道所有的错误了,接下来就看你怎么去修复它们了 :)

不过我还想提一下,你的缩进真的是一团糟,从1个空格到16个空格不等
特别是在Python中,缩进是语法的重要部分,这种情况是绝对不能接受的。

请阅读一下PEP8,了解如何规范你的代码风格。

撰写回答