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