"Build a Basic PHP Website (2018)" was retired on December 10, 2019. You are now viewing the recommended replacement.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Practice Writing Loops in Python!
You have completed Practice Writing Loops in Python!
Preview
Here's how I did it!
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
Before we look at how I solve this,
I wanna say that my solution
0:00
isn't necessarily the cleanest,
fastest, or best.
0:03
My solutions also aren't
the only solutions.
0:06
I tried to stick to solutions like
the ones you might have come up with.
0:09
But please, experiment and
0:12
see if you can find better solutions
than the ones that I'm showing.
0:13
Okay, so, problem 1 needed us
to loop through the people and
0:16
see if they like to
celebrate their birthday.
0:21
And if they do, we print out
Happy Birthday and their name.
0:23
So, first, we'll do something like for
person in BIRTHDAYS.
0:26
Because that will give us each
person as we move through the loop.
0:32
And it'll put it into
a variable called person, and
0:37
we'll move through everything that's
in BIRTHDAYS, which is what we want.
0:39
So, we're calling this a person,
not just a name, okay?
0:43
So that gives us the variable name person,
0:50
on the BIRTHDAYS as we go through
the iterations of the loop.
0:53
The first time through, it'll be James,
then Shawna, then Amaya, and so on.
0:55
Okay, so, the third item zero,
one, two, three, the third item,
0:59
so index 2 for
number three in the person tuple
1:06
is the true false boolean of whether or
not they like to celebrate their birthday.
1:10
Now if that's true, we want to print out
their name and the Happy Birthday string.
1:14
So, we can say if person[2] which is the,
this is the zero item,
1:18
this is the one item, this is the two
item, this is the three item.
1:22
So the two item, if that's true, so
just if person[2], cuz if it's true,
1:27
it will be true, if it's false,
it will be false.
1:31
Then we want to print Happy Birthday.
1:34
I'll do a comma and then a placeholder,
and I will format that with person [0],
1:38
which will get their name, all right?
1:44
So I'm gonna save that and
then I'm gonna come down here and
1:48
I'm gonna run it, python for.py.
1:51
And I should see Happy Birthday,
James, Happy Birthday, Shawna,
1:53
Happy Birthday, Kamal.
1:57
So James likes to celebrate,
Shawna likes to celebrate, and
1:58
Kamal likes to celebrate.
2:02
So, great,
that looks like we did that one.
2:03
Problem 2 is a bit trickier.
2:07
We have to look through all
the people again, but this time,
2:10
we need to calculate their half birthday.
2:13
All right, so, let's start off,
just like we did before,
2:14
with for person in BIRTHDAYS, so
that would give us each person.
2:18
Now I'm gonna make a couple
of variables here, so
2:22
that I can separate out their name and
their birthdate.
2:25
So I'm gonna do name = person[0], cuz
that's the first item in the person tuple.
2:28
And then I'm gonna do
birthdate = person[1].
2:34
And then,
I know that these are all day slash month.
2:38
So I want the day and
I want the month as their own thing.
2:43
So I'm gonna split that
on the forward slash.
2:46
So birthdate will now be a list, and the
first item on the list will be the date.
2:49
And the second item on
the list will be the month.
2:53
Cool, so now let's turn their birth month
into a number so we can do math on it.
2:56
So we'll say birthdate[1],
so the second item
3:04
in the birthdate,
is the int(birthdate[1]).
3:09
And then,
this is the place where we might differ.
3:14
So, you can look at the birthdate and
month, and
3:17
if it's bigger than six,
then just subtract six from it.
3:19
That will stop you from doing
what I'm gonna do, which is,
3:24
then I'm gonna go ahead
I'm just gonna add 6 here.
3:27
So I'm always going to add 6.
3:29
And now if birthdate[1]
is greater than 12,
3:31
which means we looped around the year,
and we don't wanna do that,
3:35
then, birthdate[1] = birthdate[1]- 12.
3:40
I can also do birthdate -= 12,
3:44
and that will give me the same output.
3:48
So, either way here,
3:52
it's up to you, whichever one
of those you would like to do.
3:53
Okay, so now,
I need to turn that back into a string
3:58
because I wanna join the two
pieces back together.
4:02
So I'm gonna change birthdate[1] to be
4:06
equal to the str(birthdate[1]), okay?
4:10
And now, I'm going to print(name,
4:15
"/".join(birthdate)).
4:21
So that will print out their name.
4:25
And then on the same line,
it will print the day and the month,
4:28
including the new month since we
changed it, joined together by a slash.
4:31
Okay, so now that should do it.
4:35
So let's test that one.
4:38
So python for.py, and
see if we can get this one on screen.
4:40
There we go.
4:48
So we'll just check a couple of these.
4:49
Let's see, let's check,
Kamal was one we figured out before.
4:51
So 29/4 should become 29/10, and it did.
4:54
And let's see, 14/3 should become,
14/9, so it did.
4:58
And 12/6 should become 12/12.
5:04
Yeah, cool, I think we did that.
5:07
All right,
let's move on to problem number 3.
5:11
So for problem number 3,
we only wanna deal with school birthdays,
5:14
only people who have birthdays
during the school year.
5:19
Now this one's kind of
a combo of problems 1 and 2.
5:23
You should know the first
couple of steps here, so
5:26
I'm not gonna really spell them out.
5:28
So for person in BIRTHDAYS,
5:30
you all know this part, name = person[0].
5:33
And birthdate =
person[1].split on the slash.
5:39
And birthdate[1] = int(birthdate[1]),
right?
5:46
That's familiar, that's what we just did.
5:53
Now I'm going to use the range function
here to get a couple of ranges of numbers.
5:56
You could do this with greater than
less than comparisons if you wanted.
6:00
So I'm gonna say if
birthdate[1] in range(1,
6:05
7), because I wanna get
the months one through six.
6:10
And ranges stop when they
get to the big number.
6:14
So this one will only go 1 to 6 and
won't include the 7.
6:17
Or birthdate[1] in range(9,
13), same idea.
6:20
Then we're going to print(name).
6:26
That should be the solution to that one.
6:29
So let's test that out.
6:31
And again,
a little bit tricky to scroll here.
6:34
Let's just run it again, there we go.
6:40
Okay, so, if their birthdays
are between 1 and 6 and 9 and 12,
6:42
so James is not, but Shawna is.
6:47
Shawna's the beginning of June, so
she has a birthday, and that comes out.
6:52
Amaya is in February,
so there's a birthday.
6:57
Kamal has one and Xan has one.
7:00
But Sam doesn't because
Sam was born in July.
7:03
James doesn't because
James was born in August.
7:06
So those two are the summer babies and
they don't have school birthdays.
7:07
So they always get that end of the year
birthday party or whatever, right?
7:12
Okay, so now let's try problem number 4.
7:17
Now I think problem number
4 might seem hard, but
7:21
I think it's actually
really pretty simple.
7:24
It's an amalgamation of a lot of
the things that we've already done.
7:27
So we need to get some data out
of each person like before.
7:30
And then we need to create a bunch of
Stars that they're celebrating and
7:33
if they're young enough.
7:35
So, this one shouldn't
introduce too many new things.
7:36
So let's say for person in BIRTHDAYS.
7:41
And again, we're gonna do the name
= person[0], age = person[-1].
7:46
So that will get the last
thing from person.
7:53
I could have also done person[3],
but -1 is just a little bit easier.
7:55
And then celebrates is
going to be person[-2].
8:00
So up here we did, what did we do?
8:06
We did person[2].
8:08
They work out to be the same thing,
8:10
whether you go from the front of
the tuple or to the back of the tuple.
8:12
And then we need to do an if condition.
8:18
So, if they celebrate,
so if celebrates and
8:20
age is less than or = 10.
8:25
And age, if we look at our
tuple again is a number, so
8:29
we don't have to convert it to anything.
8:32
So if they're under 10, we could've
also done if age is less than 11.
8:35
But I think less than or
8:39
equal to 10 is more obvious of
the logic we're trying to use.
8:41
Okay, so now, let's say stars
is equal to an empty string.
8:46
And then for star in range(age),
so for however old they are,
8:51
we're gonna make a range of that.
8:55
And then, we're going to do stars +=,
and I'm gonna use an asterisk.
8:57
Now if you wanted to use some other
fancier star, you can totally do that.
9:01
So then back here where the for loop is,
9:05
then at this point,
we're gonna say print(name, stars).
9:08
So I'm gonna print the name and
the stars on the same line.
9:13
Now, there's a much more clever way
to build the string of stars too.
9:16
I'm gonna let you find that though.
9:19
And as a hint, look up list comprehension.
9:21
Okay, so let's test this one.
9:24
And again, let's just pull this up.
9:26
And we'll do python for.py.
9:29
And if we look up here at our list,
so James is 9 and has a True.
9:34
Amaya is 8 but has a False.
9:41
And no one else is under the age of 10.
9:43
So James is the only one we should print
out, we should print out nine stars.
9:45
So there's two, there's four, there's six,
there's eight, there's nine, so
9:48
there's James and nine stars.
9:52
So great, we got all four problems.
9:55
Hopefully, you've got a bit of a better
grasp on how to use for loops now.
9:57
In the next video,
we'll check out the while loops problems.
10:00
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