1
 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

4 回答 4

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以了解如何设置代码样式。

于 2010-12-01T12:57:41.883 回答
1

当您编写return loss它时,它返回损失的,而不是变量。因此,该名称loss在调用范围内不存在。

您需要将结果分配给一个局部变量(也可以称为损失),如下所示:

loss = player()
于 2010-12-01T12:42:41.373 回答
0

loss 未在您的脚本中定义,请发布您的整个脚本以便我们可以跟踪它,这可能是您试图引用您在 player() 中使用的变量

于 2010-12-01T12:43:11.153 回答
0

您应该修复您的格式,但除此之外,如果您loss从中返回player(),您没有将其保存在任何地方。

if __name__ == "__main__":
    loss = player()   # the loss returned by player is now within the local loss
    if loss == 1: #This is where I get the NameError.
            pass
于 2010-12-01T12:43:22.800 回答