title: How to create multiple objects of a class with unique name values gathered from a list in a csv file
body: <p>I'm new to programming and I'm struggling to figure out how to get this to work. I'm trying to build a simple game called &quot;Pet pandemic&quot; and you basically just press start and the &quot;pets&quot; marked by different colored dots roam around on a grid in the window. Some of the pets spawn in with the &quot;virus&quot;. While roaming the pets will see if they are within a certain distance from one another and if so they will transmit the virus to the other who doesn't have it already, in turn updating the status of that pet to have the &quot;virus&quot; and there dot image will update to be red. The game will run until every &quot;pet&quot; has the &quot;virus&quot;. I'm struggling to implement a way to spawn the &quot;pets&quot; with unique names like &quot;Alex&quot; or &quot;Doug&quot;. I would like to use a list of names I have that's in a .csv file for this.</p>
  <p>Thank you for your time.
  Here are the files:
  <a href="https://drive.google.com/drive/folders/1tjEY6Eo1nKKtEr0st1CUFkcnBvvbiMn1?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/drive/folders/1tjEY6Eo1nKKtEr0st1CUFkcnBvvbiMn1?usp=sharing</a></p>
  <pre><code>import pygame
  import random
  import math
  import time
  
  # Window settings
  WIDTH = 960
  HEIGHT = 660
  TITLE = &quot;PET PANDEMIC&quot;
  FPS = 60
  
  #create window
  pygame.init()
  screen = pygame.display.set_mode([WIDTH, HEIGHT])
  pygame.display.set_caption(TITLE)
  clock = pygame.time.Clock()
  
  #Game Stages
  START = 0
  PLAYING = 1
  END = 2
  
  #Define colors
  WHITE = (255, 255, 255)
  BLACK = (0, 0, 0)
  BLUE = (132, 201, 217)
  
  # Load fonts
  title_font = default_font = pygame.font.Font(&quot;fonts/recharge bd.ttf&quot;, 60)
  default_font = pygame.font.Font(None, 55)
  
  # Load images
  has_v_img = pygame.image.load(&quot;red_dot.png&quot;).convert_alpha()
  no_v_img = pygame.image.load(&quot;grey_dot.png&quot;).convert_alpha()
  no_v_mask_img = pygame.image.load(&quot;black_dot.png&quot;).convert_alpha()
  
  #Game classes
  class Pet(pygame.sprite.Sprite):
      
     def __init__(self, name, x, y):
        super().__init__()
        
        self.wearing_mask = random.randint(0, 1) # 0 = no, 1 = yes.
        self.has_virus = random.randint(0, 6) # 6 = Virus
        if self.has_virus == 6:
            self.image = has_v_img
        elif self.wearing_mask != 1 and self.has_virus != 6:
            self.image = no_v_mask_img
        else:
            self.image = no_v_img
        self.rect = self.image.get_rect()
        self.rect.center = x, y
        self.name = name
        self.dir = random.randint(0, 3)
        self.is_alive = True
        self.print_name()
        
     def print_name(self):
         amnt_w_virus = 0
         if self.has_virus == 6:
           print(&quot;Spawned {} &quot;.format(self.name) + &quot;With The Virus!&quot;)
           amnt_w_virus += 1
         else:
             print(&quot;Spawned {}.&quot;.format(self.name))
         return amnt_w_virus
  
  
     def check_distance(self, other):
        distance = math.sqrt(((self.x - other.x) ** 2) + ((self.y - other.y) ** 2))
        if distance &lt; 6 and self.has_virus == 6:
                 other.has_virus = 6
                 print(self.name + &quot; Was In Range Of &quot; + other.name + &quot; And Gave Them The Virus.&quot;)
                 amnt_w_virus += 1
        else:
          return amnt_w_virus
  
     def roam(self):
            while game_running:
               if self.is_alive == True:
                  time.sleep(1.5)
                  self.dir = random.randint(0, 3)
                  self.move()
  
     def move(self):
         if self.dir == 0:
             self.rect.y += 1
             
         elif self.dir == 1:
             self.rect.x += 1
             
         elif self.dir == 2:
             self.rect.y -= 1
                 
         elif self.dir == 3:
             self.rect.x -= 1
                 
         if self.rect.left &lt; 0:
             self.rect.left = 0
                  
         elif self.rect.top &gt; HEIGHT:
             self.rect.top = HEIGHT
                 
         elif self.rect.right &gt; WIDTH:
             self.rect.right = WIDTH
                 
         print(self.name + &quot; Moved Forward In Direction &quot; + str(self.dir) + &quot; (&quot; + str(self.x) + &quot;,&quot; + str(self.y) + &quot;).&quot;)
  
         
  class Players(pygame.sprite.Group):
      
      def __init__(self, *sprites):
          super().__init__(*sprites)
          
  
  
      def update(self, *args):
          super().update(*args)
          
          self.check_distance()
          self.roam()
  
  
  # Setup
  def new_game():
      global players
      #Random Spawn Location Inside Window
      start_x = random.randint(0,WIDTH)
      start_y = random.randint(0,HEIGHT)
  
      #Open List Of Names To Use For The Pets
      f = open(&quot;names.csv&quot;, &quot;r&quot;)
      reader = csv.reader(f)
      name = []
  
      for row in reader:
          name.append(row)
          pet = {}
          #Here Im trying to spawn the pets and assign them unique names from the list but this code is all messed up
          for item in name:
              name = '{}'.format(item)
              pet[name] = pet.get(name, Pet(name=name))
              pets = Pet([name], start_x, start_y)
              #making players = a sprite group of all the pets
              players = Players(pets)
      #trying to calculate the number of pets without the virus
      players.amount_alive = len(players) - amnt_w_virus
      print(players.amnt_alive) 
  
  #grid overlay
  def draw_grid(width, height, scale):
      for x in range(0, WIDTH, scale):
          pygame.draw.line(screen, WHITE, [x, 0], [x, HEIGHT], 1)
      for y in range(0, HEIGHT, scale):
          pygame.draw.line(screen, WHITE, [0, y], [WIDTH, y], 1)
            
  def display_stats():
      infected_text = default_font.render(&quot;Infected: &quot; + str(amnt_w_virus), True, WHITE)
      rect = infected_text.get_rect()
      rect.top = 20
      rect.left = 20
      screen.blit(infected_text, rect)
  
      non_infected_text = default_font.render(&quot;Non-Infected: &quot; + str(players.amount_alive), True, WHITE)
      rect = non_infected_text.get_rect()
      rect.top = 20
      rect.right = WIDTH - 20
      screen.blit(non_infected_text, rect)
  
      time_text = default_font.render(&quot;Time: &quot; + str(time.time), True, WHITE)
      rect = time_text.get_rect()
      rect.bottom = HEIGHT - 20
      rect.left = 20
      screen.blit(time_text, rect)
  
  def start_screen():
      screen.fill(BLACK)
      
      title_text = title_font.render(TITLE, True, WHITE)
      rect = title_text.get_rect()
      rect.centerx = WIDTH // 2
      rect.bottom = HEIGHT // 2 - 15
      screen.blit(title_text, rect)
  
      sub_text = default_font.render(&quot;Press Any Key To Start The Spread&quot;, True, WHITE)
      rect = sub_text.get_rect()
      rect.centerx = WIDTH // 2
      rect.top = HEIGHT // 2 + 15 
      screen.blit(sub_text, rect)
            
  def end_screen():
      end_text = default_font.render(&quot;GAME OVER, EVERYONE IS INFECTED.&quot;, True, WHITE)
      rect = end_text.get_rect() 
      rect.centerx = WIDTH // 2
      rect.centery = HEIGHT // 2
      screen.blit(end_text, rect)
  
  # Game loop
  new_game()
  stage = START
  
  running = True
  
  while running:
      # Input handling
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              running = False
  
          elif event.type == pygame.KEYDOWN:
              if stage == START:
                  stage = PLAYING
              elif stage == END:
                  if event.key == pygame.K_r:
                      new_game()
                      stage = START
      
      # Game logic
      if stage != START:
          players.update()
  
  
      if len(players) == 0:
           stage = END
  
      # Drawing Code
      screen.fill(BLUE)
      draw_grid(960, 660, 20)
      players.draw(screen)
      display_stats()
      pygame.display.flip()
     
      if stage == START:
          start_screen()
      elif stage == END:
          end_screen()
      
          
      # Update screen
      pygame.display.update()
  
  
      # Limit refresh rate of game loop 
      clock.tick(FPS)
  
  
  # Close window and quit
  pygame.quit() ```
  </code></pre>