Python Forum
error: self.name = "NPC{}".format(num_of_NPCs + 1) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: error: self.name = "NPC{}".format(num_of_NPCs + 1) (/thread-18049.html)



error: self.name = "NPC{}".format(num_of_NPCs + 1) - Exsul - May-03-2019

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?


RE: error: self.name = "NPC{}".format(num_of_NPCs + 1) - ichabod801 - May-03-2019

You haven't specified the namespace num_of_NPCs is in, like you did on the next line.