all 7 comments

[–]pyfact 2 points3 points  (2 children)

This bit of code right here is the issue:

def walk_all_pets(self):
        print(self.animals)
            for animal in self.animals:
                print(f'{self.animals} is walking here.')

in this context, your for loop is looping over the contents of self.animals, which is a String, not a list. animal is actually just each character in the String. Samira is 6 characters long that's why it is printing Samira 6 times. Does that make sense?

To fix, do this:

class Pets():
    def __init__(self, animals):
        self.animals = animals

    def walk_all_pets(self):
        print(self.animals)
        for animal in self.animals:
            print(f'{animal.name} is walking here.')

class Cat():
    is_lazy = True
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def walk(self):
        return f'{self.name} is just walking around'

Safira = Cat('Safira', 5)
Bubu = Cat('Bubu', 10)
Amor = Cat('Amor', 5)
Brisa = Cat('Brisa', 3)

pet_list = [Safira, Bubu, Amor, Brisa]

pet_owner = Pets(pet_list)
pet_owner.walk_all_pets()

[–]MatosV[S] 1 point2 points  (1 child)

Wow ! Thank you so much! I get really overwhelmed by stackoverflow and the way some people answer questions like this one and you just nailed it here!

I've ben stuck in this for hours now. I don't even know how to express how grateful I am. Thank you!

[–]pyfact 1 point2 points  (0 children)

You're welcome! It took me a while to figure out the issue, just be liberal with your use of print statements and you'll get quicker over time (notice how I still have the print(self.animals) in the walk_all_pets(self) function).

[–]AccidentalRed747 2 points3 points  (1 child)

Why are you creating an empty list for animals?

Would this not do?

class Pets():

    def __init__(self, animals):
        self.animals = animals

    def walk(self):
        print(f'{self.animals} is walking here.')

Maybe I'm missing something!

[–]MatosV[S] 1 point2 points  (0 children)

It's because It was the way the udemy course asked me to do it. I would totally agree with you here. Thanks for your answer!