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


Messages In This Thread
error: self.name = "NPC{}".format(num_of_NPCs + 1) - by Exsul - May-03-2019, 07:23 PM

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 733 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Invalid format specifier Error Led_Zeppelin 2 7,931 Jul-11-2022, 03:55 PM
Last Post: Led_Zeppelin
  Date format error getting weekday value Aggie64 2 1,440 May-29-2022, 07:04 PM
Last Post: Aggie64
  getting error ValueError: time data '' does not match format '%H:%M' srisrinu 2 5,623 Apr-09-2020, 11:12 AM
Last Post: srisrinu
  format strings syntax error VikramSuh 2 3,553 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