开发者

Python: NameError for no apparent reason?

 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:
                        p开发者_开发知识库layer += 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"

I returned loss in player() so I don't know why I keep getting this NameError.


Let's clean that mess up and see what all the errors are:

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"

Now you know all the errors, it's up to you to fix them :)

But I also want to note that your indentation is a REAL mess, ranging from 1 space to 16 per indent.
Especially in Python were indentation is an essential part of the syntax, this is by no means tolerable.

Please read PEP8 on how to style your code.


When you write return loss it returns the value of loss, not the variable. Therefore the name loss doesn't exist in the calling scope.

You need to assign the result to a local variable (which also can be called loss) like this:

loss = player()


loss is not defined in your script, post your entire script so we can follow it through, it could be you are trying to refer to a variable you used inside player()


You should fix your formatting, but apart from that, if you returned loss from player(), you didn't save it anywhere.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜