0

I want to create multiple pet objects by making it a loop. here is what I got so far, I have Class Dog, Class Fish, Class Cat, Class Bird. class Dog:

def __init__(self,species,name,birthedate,breed,color):
    self.__species = species
    self.__name = name
    self.__birthdate = birthedate
    self.__breed = breed
    self.__color = color

def get_species(self):
    return self.__species

def get_name(self):
    return self.__name

def get_birthdate(self):
    return self.__birthdate

def get_breed(self):
    return self.__breed

def get_color(self):
    return self.__color

import dog
import cat
import fish
import bird
import csv

with open('C:\pet.csv', 'r') as f:
  reader = csv.reader(f)
for row in reader:
    if row[0]=="Dog":
        my_list.append(dog.Dog(row[0],row[1],row[2],row[3],row[4]))
    if row[0]=="Cat":

here I don't know how to create objects for each dog,bird,fish,cat I get from my csv.file.

J.Johnff
  • 1
  • 1

1 Answers1

0

Looks like you're on the right track. I think you forgot to include the initialization of my_list before you start the reader loop. In that case, all your objects would be stored in your my_list.

Bonus tip: you may want to look into unpacking. Then you'll be able to instantiate your objects without specifiying each column: ``

my_list = list()
for row in reader:
    if row[0]=="Dog":
        my_list.append(dog.Dog(*row))
    ...
Justin Bell
  • 800
  • 7
  • 20