Python Forum

Full Version: error: self.name = "NPC{}".format(num_of_NPCs + 1)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
class NPC:

    num_of_NPCs = 0

    def __init__(self):
        self.name = "NPC{}".format(num_of_NPCs + 1)
        NPC.num_of_NPCs += 1
Error:
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?
You haven't specified the namespace num_of_NPCs is in, like you did on the next line.