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 now that you've learned all about if, elif and else,
2
as well as nested if statements,
3
then I've got two code challenges coming up for you. In the first one, in the
4
code challenge that's called day 3.2 BMI calculator
5
2.0,
6
this is going to be an upgraded version of the BMI calculator that you created
7
yesterday.
8
Instead of just working out the BMI value of somebody,
9
we're going to give them an interpretation.
10
You might've come across a BMI chart that looks like this,
11
where it divides up people's BMI and tells them whether if the underweight,
12
normal weight, overweight, et cetera. And this depends on the value of the BMI.
13
So if the value of the BMI is less than 18.5,
14
then they are underweight. If they're over 18.5,
15
but below 25, then they have a normal weight.
16
So your job is to not only create a BMI calculator,
17
but also to give them the corresponding interpretation message.
18
Now it's really,
19
really important that you use the words here that are highlighted in bold in your
20
interpretation, that based on a particular height and weight,
21
you actually give them the correct interpretation.
22
Pause the video and try to give this challenge a go.
23
All right. So how did you get on?
24
There was a little bit of a revision from what you've done previously.
25
Hopefully you've been able to calculate the BMI without much trouble and then
26
use what you learned about if, elif and else to provide the user with the
27
corresponding interpretation for the BMI that they have.
28
Let's start out by calculating the BMI. And the BMI,
29
according to this equation is equal to the weight in kilograms
30
divided by the height squared. So we've got the weight already,
31
and then we divided by the height to the power of two.
32
Now, notice how I've already converted these inputs to floats.
33
So we don't have to do what we did previously when we were calculating the BMI.
34
Now what the exercise does mention though
35
is that we should round the result of the nearest whole number.
36
But you probably remember how to do this right? Using the round function.
37
So if we wrap the entire equation inside a round function,
38
then it's going to round it to the nearest whole number.
39
So if this is what you got stuck on, then head back to your code and just fix it.
40
Now, once we've calculated the rounded value of BMI,
41
the next step is to give the user an interpretation message based on their BMI.
42
So let's start with the first one.
43
If the BMI is less than 18.5,
44
well then in this case, they're actually underweight, right?
45
So it will print something like your BMI is,
46
and then inside here we'll insert the value of their BMI.
47
And this of course is using f-strings, so let's add an F to the beginning.
48
Your BMI is this particular value and you are underweight.
49
Now,
50
what if their BMI was between 18.5 and 25?
51
Well, because we've already checked that their BMI is less than 18.5,
52
then if this is false because it's jumped to the next level,
53
then we don't actually have to check this.
54
All we have to do is check that it's still below 25. And to do that,
55
we're going to use an Elif.
56
So elif BMI is less than 25, well then in this case,
57
we're going to print you bmi
58
is this particular value, and you have a normal
59
weight. If this is a little bit mysterious to you,
60
why it is that I'm not checking that their BMI is over 18.5,
61
but below 25 and I'm simply just checking that it's less than 25,
62
then I recommend having a play around with the code as it is right now,
63
and trying out some different values for BMI.
64
Just change this to say 18,
65
and then try maybe something like 22 so that it's caught by each of these
66
conditions. And then you'll soon realize that this
67
if, elif, else is an entire group of statements,
68
and it's going to check this one first and only if this fails,
69
when it's false, does it go and check the next one.
70
So then this is only going to be printed when their BMI is over 18.5,
71
but below 25. And once you're comfortable with this,
72
then the rest of the statements should be pretty easy.
73
All right,
74
Now,
75
once we've reached here and their BMI is not less than 35 and we want to catch the
76
remaining cases where the BMI is over 35,
77
then all we have to do is add an else statement.
78
That's all we have to do.
79
We've managed to check for those people that have BMI less than 18.5.
80
If it's not less 18.5, well, is it less than 25?
81
So this will print when the BMI is greater than 18.5,
82
but less than 25. And we've done that for all of these separate statements.
83
If you struggled with this code,
84
then I recommend having a look back at the previous lesson where we talked more
85
in detail in terms of if, elif and else.
86
If you struggled with this challenge,
87
then have a go at pasting the code that's in the solution into the Thonny code
88
editor. And instead of having the BMI being a workout value,
89
just set it to some different numbers. So for example, if we start out testing,
90
if the BMI is equal to 18,
91
which of these will it fall into, then we can click on the debug step into each
92
of these. And you can see that when it evaluates this condition,
93
is the BMI less than 18.5, well,
94
18 is less than 18.5. So this is true.
95
And in that case, it's going to execute this line of code.
96
So it is going to print out your BMI is 18, you are underweight.
97
And then it's done. That's the end of the code. Now,
98
if we change this to say 22 however, and we click on debug,
99
then it's going to evaluate the first condition, is 22 less than 18.5 and
100
that's going to be false.
101
So then it skips to the next elif that it can find
102
and it checks whether if the BMI 22 is less than 25.
103
So now it's going to execute this print statements.
104
Have a play around with that
105
if it's still a little bit mysterious to you and hopefully,
106
once you're done there,
107
then you'll be ready to move on to the next code challenge
108
which is a little bit more challenging. For all of that and more,
109
I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.