Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Walkthrough a solution to the challenge.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:01
How did you do?
0:02
Let's walk through
the solution I have here.
0:03
First, we needed to add a dunder string
method to Dog that returns the Pet's name.
0:07
So, if I scroll down here a bit,
0:12
you can see I added it right
under the init method.
0:15
Dunder string, take self, and
0:19
I'm just returning a f string that I just
decided to do Pet name with a colon.
0:21
And then put in {self.name} to access
our name instance attribute and
0:26
have that printed to the screen.
0:30
Next was to add a dunder equals
method to Dog that checks if
0:34
two dog's names are the same.
0:38
Right after our dunder string method,
I added our dunder equals.
0:43
It takes self, and
the other thing you're comparing it to.
0:46
So, then I return if self.name, which is
our instance attribute equals other.name,
0:50
which will access the other Dog we're
comparing it to, their name attribute.
0:57
And it will tell us whether or
not they are equal.
1:03
So, this will return true or false.
1:06
Last, we needed to add a dunder
iter method to Shelter to iterate
1:11
through the animals list.
1:15
If I scroll down,
you can see that we created, or
1:18
you were given the self.animals instance
attribute, which was set to an empty list.
1:22
And then right after our init method
I added our dunder iter take (self).
1:30
And I use that fancy Python
keyword yield from to
1:35
iterate through and return self.animals.
1:40
So, remember yield from
goes into our list, and
1:45
we'll return each animal inside
of this list one by one.
1:50
And then of course, you still have
the add animal method that you were
1:55
given to add animals to your
animal list here in your shelter.
2:00
Now if I scroll down, all this code should
be the same, should still have three dogs.
2:06
If you decide to change it,
totally fine, that's up to you.
2:10
We have created our shelter, and then we
added our three pets to our shelter list.
2:14
Now inside of this Loop,
we are looping through our pets.
2:20
This part right here will
activate the dunder iter method.
2:26
And then our if statements or
our conditional statements, the if and
2:34
else here, will activate, if scroll
back up, our dunder equal method.
2:39
And then printing out pet,
which will be each
2:48
individual dog that's been
added to our shelter.
2:52
That will activate
the dunder string method.
2:58
So, we'll get this printed
to the console right here,
3:03
the Pet's name with this little Pet name,
colon at the front.
3:06
So, if we have three dogs, Maddie,
Jetro, and Luna added in that order,
3:11
as we loop through them,
the first pet that it will grab is Maddie,
3:16
which should be equal to itself.
3:21
So, we should get (equal) and
then (Maddie).
3:24
And then the other two obviously have
different names, (jethro), and (luna).
3:27
So, they do not have the name of maddie.
3:31
So, our else will run,
we'll get (''not equal''), and
3:34
then their names will be
printed to the console.
3:38
So, let's test it.
3:41
I'm gonna scroll this up, python pets.py,
3:44
hit Enter, and we get equal.
3:49
Because Maddie is equal to Maddie,
we get not equal,
3:52
cuz Jethro is not equal to Maddie, not
equal because Luna is not equal to Maddie.
3:56
How did you do?
4:02
Did you get it all worked out?
4:03
Keep practicing if not.
4:05
I know you've got this.
4:07
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up