Python: NameError for no apparent reason? -


 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 nameerror.                 pass         else:                 computer()                 if computer > player:                         print "ich habe gewonnen!"                 else:                         print "du hast gewonnen" 

i returned loss in player() don't know why keep getting nameerror.

let's clean mess , see errors are:

from random import random # tip, check out randint (the function in random module)  def computer():     computer = 0     while computer < 21:         val = int(random()*10)+1 # please use 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 function (hint need call function)                              # you're comparing object against string                              # yield false, therefore player                              # never gets chance enter number              # never gets executed             player += hval             if player > 21:                 print "du hast verloren!"                 loss = 1                 break              else:                 continue          # gets executed         else:             player += hval             break      return player # returns value of player     return loss # never reached, dead code, return above has left function  if __name__ == "__main__":     player() # returns integer, doesn't assign name      if loss == 1: # loss not defined in scope         pass      else:         computer() # again, doesn't assign returned value      if computer > player: # if reached, fail yet again                           # no name error time, you're comparing function object                           # against player object, (at least in cpython)                           # compare memory addresses          print "ich habe gewonnen!"      else:         print "du hast gewonnen" 

now know errors, it's fix them :)

but want note indentation real mess, ranging 1 space 16 per indent.
in python indentation essential part of syntax, no means tolerable.

please read pep8 on how style code.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -