Afrikaans
Akan
Albanian
Amharic
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
French
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
So you should see day 2.3 Your Life in Weeks, and this is the challenge that we're going to complete
in this lesson.
The idea is that there's a really great article by Tim Urban of Wait but Why on visualizing your
life in weeks.
And I was reading this recently, and it really made an impact on me, especially when you see that
a whole life from age 0 to 90 can be mapped out like this in weeks where every week is represented
by a little checkbox and you can see each of these boxes right?
This is like how many weeks you have in your whole life, and it's not as small as I thought it would
be.
I thought each of these boxes would be like invisible because they're so tiny because
I have so many weeks in my life, but it's actually not true.
There's actually not that many.
So in today's coding challenge, what I want you to do is to create a program using the mathematical
operations that you learned about, as well as the fstrings that you learned about in the last lesson
to tell us how many days, weeks and months we actually have left
if we were lucky enough to live until 90 years old. The code is going to ask you for your current age,
let's say that I'm 12, and then it's going to calculate the number of days, weeks and months you have
left in your life if you lived until 90. And then it's going to print it out to you
like so.
And the important premise of this code challenge for all of the test to pass is that there are 365 days
in the year, 52 weeks in a year and 12 months in a year.
I know that because of leap year, it's actually a little bit more than that,
but for this exercise sake, let's just call it 365, 52 and 12.
Pause the video and see if you can complete this challenge and then head back over here and we'll walk through
the solution together.
All right.
How did that exercise go?
I'm sorry, it's a little bit dark making you think about how many days, weeks and months you have left
in life, but you can think about it as a motivation, right?
If I only have 400 months left, I don't know if I want to spend one of those months just watching Game
of Thrones.
Maybe I want to do something for other people.
Maybe I want to create something.
So how are we going to calculate this?
Well, we've got hold of the age from the input, but you might remember from previous lessons that
the input always creates a string data type.
In order to do your maths, we do still have to do a type conversion, so we have to change our age
into a number.
So let's call it age_as_int, age is never going to be a float, right?
Because not many people are going to tell you that they are 56.4 years old, so we're
going to accept this as a whole number. So we'll convert our age into an int.
Now the next stage is we have to do some calculations.
We have to calculate the number of years left, right?
So let's call it years_remaining and that's going to be equal to 90,
so we're presuming we're going to live to 90, minus age_as_int.
So now that we've got the number of years remaining, we can calculate the number of days, weeks and
months left.
So days remaining is going to be equal to years remaining multiplied by 365 because there
are 365 days in a year, and then we can do the same for weeks.
And months.
So now that I've got hold of the number of days remaining, weeks remaining and months remaining, then
I'm ready to actually use it inside my string.
But before we do that, it's always helpful to check your code as you go along to make sure that it
works exactly as you expect it to.
So let's go ahead and try to print what is the number of months remaining.
So if we go ahead and run our code and let's say that we're 80 years old, right?
Which means that we'll have 10 years remaining and we'll have 120 months remaining.
So all of that looks pretty good to me.
So the next thing we have to do is to somehow output this sentence where it says you have x days, y weeks
and z months left.
So let's copy that and paste that sentence in here.
And then I'm going to turn it into a string by wrapping it inside a set of double-quotes.
Now that I've got a string, I can turn it into an fstring by putting the card to 'f' before the string.
And instead of having x, y and z, I can replace them with some curly braces inside of which I can
put my variable names.
So I'll change this to days_remaining, this to weeks_remaining and this to months_remaining.
So now that I've got my string, I have two options: I can either just wrap this inside a print statement
and print it as it is.
But what I think might actually make it easier to read is to save it inside a separate variable called
message.
And then in the next line, I can simply print my message.
It does exactly the same thing, but I think this is a little bit easier to read.
So now let's go ahead and run our code and see if we get any errors.
It's asking us for our current age, let's say, was 67 years old.
Well, then we only have 8,395 days and 1,196 weeks and 276 months.
So I hope you managed to get this code challenge right
and even though the challenge is a little bit dark,
well, at least remember that when you become a really good programmer, you'll be able to cut down
on a lot of the boring things and repetitive tasks in life.
So at least you'll be claiming back some of those days, weeks and months.
So I hope you had fun with me on this code challenge. In the next lesson,
I've got a quiz for you. So head over there and give it a go.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.