0

So, I have list of objects that have attributes ["lname","fname","gender",age] and I need to delete a customer using the class destructor based off of input of "fname lname". When I attempt this I get an error message: "TypeError: list indices must be integers or slices, not FitClinic". How do I fix this? This is what I have so far:

class FitClinic:
    def __init__(self, lname, fname, gender, age):
        self.lname = lname
        self.fname = fname
        self.gender = gender
        self.age = int(age) 

    def __del__(self):
        print("Customer has been deleted")

    def get_lname(self):
        return self.lname

    def get_fname(self):
        return self.fname

    def get_gender(self):
        return self.gender

    def get_age(self):
        return self.age

fh=open('fit_clinic_20.csv', 'r')
fh.seek(3)
listofcustomers=[]
for row in fh:
    c = row.split(",")
    listofcustomers.append(FitClinic(c[0], c[1], c[2], c[3])) 

def bubblesort(listofcustomers):
    for i in range(len(listofcustomers)):
        for j in range(0, len(listofcustomers) -i -1):
            if listofcustomers[j].get_lname()>listofcustomers[j+1].get_lname():
                listofcustomers[j],listofcustomers[j+1] = listofcustomers[j+1],listofcustomers[j]
    return listofcustomers

def printlist():
    for c in listofcustomers:
        print("\t",c.get_lname(),c.get_fname(),c.get_gender(),c.get_age())

x=[]
x=input("Please enter the first and lastname of the customer you wish to remove: ")
x=x.split(" ")
for c in listofcustomers:
    if x[0] == listofcustomers[c].get_fname() and x[1] == listofcustomers[c].get_lname():
        del listofcustomers[c]
print("List of customers:")
bubblesort(listofcustomers)
printlist()
osmans
  • 35
  • 3

1 Answers1

0

You are using a for loop so each time the variable c is assigned an FitClinic instance, so you cannot use it as an index in a list, this would work:

for i,_ in enumerate(listofcustomers):
    if x[0] == listofcustomers[i].get_fname() and x[1] == listofcustomers[i].get_lname():
        del listofcustomers[i]

marcos
  • 4,473
  • 1
  • 10
  • 24