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

損失は​​スクリプトで定義されていません。スクリプト全体を投稿して、それを追跡できるようにします。player() 内で使用した変数を参照しようとしている可能性があります。

于 2010-12-01T12:43:11.153 に答える
0

フォーマットを修正する必要がありますが、それとは別に、から戻った場合はlossplayer()どこにも保存していません。

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 に答える