Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
So you should see a day 2.3, Your Life in Weeks
2
and this is the challenge that we're going to complete in this lesson.
3
The idea is that there's a really great article by Tim Urban of wait
4
but why on visualizing your life in weeks.
5
And I was reading this recently and it really made an impact on me,
6
especially when you see that a whole life from age zero to 90 can be mapped out
7
like this in weeks where every week is represented by little check box.
8
And you can see each of these boxes, right?
9
This is like how many weeks you have in your whole life.
10
And it's not as small as I thought it would be.
11
I thought each of these boxes would be like invisible because they're so tiny
12
because I have so many weeks in my life. But it's actually not true.
13
There's actually not that many. So in today's coding challenge,
14
what I want you to do is to create a program using the mathematical operations
15
that you've learned about as well as the f-strings that you learned about in the
16
last lesson to tell us how many days, weeks, and months we actually have left
17
if we were lucky enough to live until 90 years old.
18
The code is going to ask you for your current age,
19
let's say that I'm 12.
20
And then it's going to calculate the number of days,
21
weeks, and months you have left in your life
22
if you lived until 90.
23
And then it's going to print it out to you like so.
24
And the important premise of this code challenge for all of the tests to pass is
25
that there are 365 days in a year,
26
52 weeks in a year and 12 months in a year. I know that because of leap year,
27
it's actually a little bit more than that. But for this exercise sake,
28
let's just call it for 365, 52 and 12.
29
Pause the video and see if you can complete this challenge and then head back
30
over here and we'll walk through the solution together.
31
All right. How did that exercise go? I'm sorry
32
it's a little bit dark making you think about how many days, weeks,
33
and months you have left in life, but you can think about it as a motivation,
34
right? If I only have 400 months left,
35
I don't know if I want to spend one of those months just watching game of
36
Thrones. Maybe I want to do something for other people.
37
Maybe I want to create something. So how are we going to calculate this?
38
Well, we've got a hold of the age from the input,
39
but you might remember from previous lessons that the input always creates a
40
string datatype. In order to do math,
41
we do still have to do a type conversion.
42
So we have to change our age into a number. So let's call it age_
43
as_int. Age is never going to be a float, right?
44
Because not many people are going to tell you that they are 56.4 years old.
45
So we're going to accept this as a whole number.
46
So we'll convert our age into an int.
47
Now the next stage is we have to do some calculations. We have to calculate,
48
um, the number of years left, right?
49
So let's call it years_remaining and that's going to be equal to 90,
50
so we're presuming we're going to live to 90, minus age_
51
as_int. So now that we've got the number of years remaining,
52
we can calculate the number of days, weeks, and months left.
53
So days_remaining is going to be equal to years_remaining multiplied by
54
55
because there are 365 days in a year.
56
And then we can do the same for weeks,
57
and months.
58
So now that I've got hold of the number of days remaining, weeks remaining and
59
months remaining, then I'm ready to actually use it inside my string.
60
But before we do that,
61
it's always helpful to check your code as you go along to make sure that it
62
works exactly as you expect it to.
63
So let's go ahead and try to print what is the number of months remaining.
64
So if we go ahead and run our code,
65
and let's say that we're 80 years old, right?
66
Which means that we'll have 10 years remaining and we will have 120 months
67
remaining. So all of that looks pretty good to me.
68
So the next thing we have to do is to somehow output the sentence where it says
69
you have X days, Y weeks and Z months left.
70
So let's copy that and paste that sentence in here.
71
And then I'm going to turn it into a string by wrapping it inside a set of
72
double quotes. Now that I've got a string,
73
I can turn it into an f-string by putting the character F before the string.
74
And instead of having X, Y, and Z,
75
I can replace them with some curly braces inside of which I can put my variable
76
names. So I'll change this to days_remaining,
77
this to weeks_remaining,
78
and this to months_remaining.
79
And now that I've got my string, I have two options.
80
I can either just wrap this inside a print statement and print it as it is.
81
But what I think might actually make it easier to read is to save it inside a
82
separate variable, called message. And then in the next line,
83
I can simply print my message. It does exactly the same thing,
84
but I think this is a little bit easier to read.
85
So now let's go ahead and run our code and see if we get any errors.
86
It's asking us for our current age, let's say we're 67 years old. Well,
87
then we only have 8,395 days and 1,196 weeks and 276 months.
88
So I hope you managed to get this code challenge right.
89
And even though the challenge is a little bit dark, well,
90
at least remember that when you become a really good programmer,
91
you'll be able to cut down on a lot of the boring things and repetitive tasks in
92
life. So at least you will be claiming back some of those days, weeks,
93
and months.
94
So I hope you had fun with me on this code challenge. In the next lesson
95
I've got a quiz for you. So head over there and to give it a go.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.