Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,360 --> 00:00:07,230
So you should see day 2.3 Your Life in Weeks, and this is the challenge that we're going to complete
2
00:00:07,230 --> 00:00:08,189
in this lesson.
3
00:00:09,450 --> 00:00:16,770
The idea is that there's a really great article by Tim Urban of Wait but Why on visualizing your
4
00:00:16,770 --> 00:00:17,850
life in weeks.
5
00:00:18,060 --> 00:00:23,730
And I was reading this recently, and it really made an impact on me, especially when you see that
6
00:00:24,060 --> 00:00:34,500
a whole life from age 0 to 90 can be mapped out like this in weeks where every week is represented
7
00:00:34,500 --> 00:00:38,910
by a little checkbox and you can see each of these boxes right?
8
00:00:38,920 --> 00:00:44,250
This is like how many weeks you have in your whole life, and it's not as small as I thought it would
9
00:00:44,250 --> 00:00:44,370
be.
10
00:00:44,370 --> 00:00:48,050
I thought each of these boxes would be like invisible because they're so tiny because
11
00:00:48,090 --> 00:00:50,760
I have so many weeks in my life, but it's actually not true.
12
00:00:50,910 --> 00:00:51,990
There's actually not that many.
13
00:00:52,530 --> 00:00:59,340
So in today's coding challenge, what I want you to do is to create a program using the mathematical
14
00:00:59,340 --> 00:01:04,620
operations that you learned about, as well as the fstrings that you learned about in the last lesson
15
00:01:05,010 --> 00:01:08,580
to tell us how many days, weeks and months we actually have left
16
00:01:08,940 --> 00:01:16,230
if we were lucky enough to live until 90 years old. The code is going to ask you for your current age,
17
00:01:17,280 --> 00:01:26,100
let's say that I'm 12, and then it's going to calculate the number of days, weeks and months you have
18
00:01:26,100 --> 00:01:31,950
left in your life if you lived until 90. And then it's going to print it out to you
19
00:01:32,220 --> 00:01:32,940
like so.
20
00:01:34,370 --> 00:01:40,550
And the important premise of this code challenge for all of the test to pass is that there are 365 days
21
00:01:40,550 --> 00:01:44,150
in the year, 52 weeks in a year and 12 months in a year.
22
00:01:44,690 --> 00:01:47,900
I know that because of leap year, it's actually a little bit more than that,
23
00:01:47,900 --> 00:01:53,030
but for this exercise sake, let's just call it 365, 52 and 12.
24
00:01:54,140 --> 00:01:59,600
Pause the video and see if you can complete this challenge and then head back over here and we'll walk through
25
00:01:59,600 --> 00:02:00,500
the solution together.
26
00:02:05,020 --> 00:02:05,410
All right.
27
00:02:05,440 --> 00:02:06,820
How did that exercise go?
28
00:02:06,940 --> 00:02:11,590
I'm sorry, it's a little bit dark making you think about how many days, weeks and months you have left
29
00:02:11,920 --> 00:02:14,560
in life, but you can think about it as a motivation, right?
30
00:02:15,070 --> 00:02:19,780
If I only have 400 months left, I don't know if I want to spend one of those months just watching Game
31
00:02:19,780 --> 00:02:20,200
of Thrones.
32
00:02:20,650 --> 00:02:22,450
Maybe I want to do something for other people.
33
00:02:22,460 --> 00:02:23,710
Maybe I want to create something.
34
00:02:25,030 --> 00:02:26,920
So how are we going to calculate this?
35
00:02:27,130 --> 00:02:33,970
Well, we've got hold of the age from the input, but you might remember from previous lessons that
36
00:02:33,970 --> 00:02:37,210
the input always creates a string data type.
37
00:02:37,780 --> 00:02:43,450
In order to do your maths, we do still have to do a type conversion, so we have to change our age
38
00:02:43,450 --> 00:02:44,590
into a number.
39
00:02:44,590 --> 00:02:49,480
So let's call it age_as_int, age is never going to be a float, right?
40
00:02:50,230 --> 00:02:55,180
Because not many people are going to tell you that they are 56.4 years old, so we're
41
00:02:55,180 --> 00:03:00,730
going to accept this as a whole number. So we'll convert our age into an int.
42
00:03:01,750 --> 00:03:04,900
Now the next stage is we have to do some calculations.
43
00:03:05,380 --> 00:03:09,340
We have to calculate the number of years left, right?
44
00:03:09,340 --> 00:03:15,480
So let's call it years_remaining and that's going to be equal to 90,
45
00:03:15,490 --> 00:03:20,920
so we're presuming we're going to live to 90, minus age_as_int.
46
00:03:21,670 --> 00:03:26,680
So now that we've got the number of years remaining, we can calculate the number of days, weeks and
47
00:03:26,680 --> 00:03:27,520
months left.
48
00:03:27,520 --> 00:03:37,720
So days remaining is going to be equal to years remaining multiplied by 365 because there
49
00:03:37,720 --> 00:03:44,170
are 365 days in a year, and then we can do the same for weeks.
50
00:03:47,780 --> 00:03:49,100
And months.
51
00:03:52,190 --> 00:03:59,150
So now that I've got hold of the number of days remaining, weeks remaining and months remaining, then
52
00:03:59,150 --> 00:04:01,550
I'm ready to actually use it inside my string.
53
00:04:01,790 --> 00:04:07,790
But before we do that, it's always helpful to check your code as you go along to make sure that it
54
00:04:07,790 --> 00:04:09,680
works exactly as you expect it to.
55
00:04:10,130 --> 00:04:16,640
So let's go ahead and try to print what is the number of months remaining.
56
00:04:17,149 --> 00:04:22,490
So if we go ahead and run our code and let's say that we're 80 years old, right?
57
00:04:22,820 --> 00:04:27,910
Which means that we'll have 10 years remaining and we'll have 120 months remaining.
58
00:04:27,920 --> 00:04:30,260
So all of that looks pretty good to me.
59
00:04:30,830 --> 00:04:37,490
So the next thing we have to do is to somehow output this sentence where it says you have x days, y weeks
60
00:04:37,490 --> 00:04:39,020
and z months left.
61
00:04:39,200 --> 00:04:41,990
So let's copy that and paste that sentence in here.
62
00:04:42,500 --> 00:04:47,900
And then I'm going to turn it into a string by wrapping it inside a set of double-quotes.
63
00:04:48,620 --> 00:04:54,770
Now that I've got a string, I can turn it into an fstring by putting the card to 'f' before the string.
64
00:04:55,310 --> 00:05:02,270
And instead of having x, y and z, I can replace them with some curly braces inside of which I can
65
00:05:02,270 --> 00:05:04,100
put my variable names.
66
00:05:04,550 --> 00:05:13,610
So I'll change this to days_remaining, this to weeks_remaining and this to months_remaining.
67
00:05:17,680 --> 00:05:23,350
So now that I've got my string, I have two options: I can either just wrap this inside a print statement
68
00:05:23,350 --> 00:05:24,520
and print it as it is.
69
00:05:24,850 --> 00:05:30,730
But what I think might actually make it easier to read is to save it inside a separate variable called
70
00:05:30,730 --> 00:05:31,270
message.
71
00:05:31,720 --> 00:05:35,110
And then in the next line, I can simply print my message.
72
00:05:35,260 --> 00:05:39,040
It does exactly the same thing, but I think this is a little bit easier to read.
73
00:05:39,730 --> 00:05:43,330
So now let's go ahead and run our code and see if we get any errors.
74
00:05:44,950 --> 00:05:48,640
It's asking us for our current age, let's say, was 67 years old.
75
00:05:49,270 --> 00:05:57,070
Well, then we only have 8,395 days and 1,196 weeks and 276 months.
76
00:05:58,240 --> 00:06:00,790
So I hope you managed to get this code challenge right
77
00:06:01,120 --> 00:06:03,910
and even though the challenge is a little bit dark,
78
00:06:04,300 --> 00:06:08,650
well, at least remember that when you become a really good programmer, you'll be able to cut down
79
00:06:08,980 --> 00:06:12,160
on a lot of the boring things and repetitive tasks in life.
80
00:06:12,490 --> 00:06:16,240
So at least you'll be claiming back some of those days, weeks and months.
81
00:06:16,900 --> 00:06:21,130
So I hope you had fun with me on this code challenge. In the next lesson,
82
00:06:21,160 --> 00:06:24,580
I've got a quiz for you. So head over there and give it a go.
8527
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.