Python Forum
error: self.name = "NPC{}".format(num_of_NPCs + 1)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error: self.name = "NPC{}".format(num_of_NPCs + 1)
#1
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?
Reply
#2
You haven't specified the namespace num_of_NPCs is in, like you did on the next line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 656 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Invalid format specifier Error Led_Zeppelin 2 7,588 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Date format error getting weekday value Aggie64 2 1,378 May-29-2022, 07:04 PM
Last Post: Aggie64
  getting error ValueError: time data '' does not match format '%H:%M' srisrinu 2 5,547 Apr-09-2020, 11:12 AM
Last Post: srisrinu
  format strings syntax error VikramSuh 2 3,479 Sep-06-2018, 11:41 AM
Last Post: VikramSuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020