May-03-2019, 07:23 PM
Code:
I'm trying to name class instances "NPC" plus an increasing number (1, 2, 3) each time a new class instance is created. The above code gives me a name error. What am I doing wrong?
class NPC: num_of_NPCs = 0 def __init__(self): self.name = "NPC{}".format(num_of_NPCs + 1) NPC.num_of_NPCs += 1Error:
Quote:NameError: name 'num_of_NPCs' is not defined
I'm trying to name class instances "NPC" plus an increasing number (1, 2, 3) each time a new class instance is created. The above code gives me a name error. What am I doing wrong?