简体   繁体   中英

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:
                        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"

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()

在你的脚本中没有定义丢失,发布整个脚本以便我们可以跟进它,可能是你试图引用你在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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM