SHARE
TWEET

Untitled

a guest Dec 16th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Traceback (most recent call last):
  2.   File "test.py", line 32, in <module>
  3.     invoker = Hero("Invoker")
  4.   File "test.py", line 14, in __init__
  5.     Alive.__init__(self)
  6.   File "test.py", line 9, in __init__
  7.     self.id = totalId + 1
  8. UnboundLocalError: local variable 'totalId' referenced before assignment
  9.    
  10. import random
  11.  
  12. totalId = -1
  13.  
  14. class Alive:
  15.     def __init__(self):
  16.         teams = ["Honor", "BlackLoop"]
  17.         self.team = random.choice(teams)
  18.         self.id = totalId + 1
  19.         totalId = totalId + 1
  20.  
  21. class Hero(Alive):
  22.     def __init__(self, name = "default", lvl = 1):
  23.         Alive.__init__(self)
  24.         self.lvl = lvl
  25.         self.name = name
  26.  
  27.     def lvlUp(self, count = 1):
  28.         self.lvl = self.lvl + count
  29.  
  30.     def getInfo(self):
  31.         print(f"{'*' * 20}nId_{self.id}nName: {self.name}nTeam: {self.team}nLevel: {self.lvl}n{'*' * 20}nn")
  32.  
  33. class Soldier(Alive):
  34.     def followTheHero(self):
  35.         pass
  36.  
  37. if __name__ == "__main__":
  38.     honor = []
  39.     blackloop = []
  40.  
  41.     invoker = Hero("Invoker")
  42.     void = Hero("Void")
  43.  
  44.     invoker.getInfo()
  45.     void.getInfo()
  46.    
  47. class Alive:
  48.     def __init__(self):
  49.         global totalId                       ##### нужно добавить #####
  50.         teams = ["Honor", "BlackLoop"]
  51.         self.team = random.choice(teams)
  52.         self.id = totalId + 1
  53.         totalId = totalId + 1
  54.    
  55. class Alive:
  56.  
  57.     total_id = -1
  58.  
  59.     def __init__(self):
  60.         teams = ["Honor", "BlackLoop"]
  61.         self.team = random.choice(teams)
  62.         Alive.total_id += 1
  63.         self.id = Alive.total_id
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top