Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,240 --> 00:00:03,870
So now that you've learned all about if, elif and else,
2
00:00:04,260 --> 00:00:06,150
as well as nested if statements,
3
00:00:06,330 --> 00:00:11,330
then I've got two code challenges coming up for you. In the first one, in the
4
00:00:12,000 --> 00:00:16,020
code challenge that's called day 3.2 BMI calculator
5
00:00:16,230 --> 00:00:17,100
2.0,
6
00:00:17,730 --> 00:00:22,730
this is going to be an upgraded version of the BMI calculator that you created
7
00:00:23,190 --> 00:00:24,023
yesterday.
8
00:00:24,630 --> 00:00:28,260
Instead of just working out the BMI value of somebody,
9
00:00:28,590 --> 00:00:30,810
we're going to give them an interpretation.
10
00:00:31,650 --> 00:00:34,680
You might've come across a BMI chart that looks like this,
11
00:00:35,280 --> 00:00:40,280
where it divides up people's BMI and tells them whether if the underweight,
12
00:00:41,340 --> 00:00:46,020
normal weight, overweight, et cetera. And this depends on the value of the BMI.
13
00:00:46,230 --> 00:00:50,100
So if the value of the BMI is less than 18.5,
14
00:00:50,490 --> 00:00:54,960
then they are underweight. If they're over 18.5,
15
00:00:54,990 --> 00:00:58,350
but below 25, then they have a normal weight.
16
00:00:58,920 --> 00:01:03,240
So your job is to not only create a BMI calculator,
17
00:01:03,840 --> 00:01:08,670
but also to give them the corresponding interpretation message.
18
00:01:09,330 --> 00:01:10,170
Now it's really,
19
00:01:10,170 --> 00:01:14,820
really important that you use the words here that are highlighted in bold in your
20
00:01:14,820 --> 00:01:18,480
interpretation, that based on a particular height and weight,
21
00:01:18,540 --> 00:01:21,360
you actually give them the correct interpretation.
22
00:01:22,500 --> 00:01:25,680
Pause the video and try to give this challenge a go.
23
00:01:29,370 --> 00:01:31,140
All right. So how did you get on?
24
00:01:31,380 --> 00:01:34,560
There was a little bit of a revision from what you've done previously.
25
00:01:34,920 --> 00:01:38,970
Hopefully you've been able to calculate the BMI without much trouble and then
26
00:01:39,000 --> 00:01:43,800
use what you learned about if, elif and else to provide the user with the
27
00:01:43,800 --> 00:01:47,640
corresponding interpretation for the BMI that they have.
28
00:01:48,480 --> 00:01:52,620
Let's start out by calculating the BMI. And the BMI,
29
00:01:52,680 --> 00:01:55,860
according to this equation is equal to the weight in kilograms
30
00:01:55,860 --> 00:01:59,580
divided by the height squared. So we've got the weight already,
31
00:01:59,790 --> 00:02:04,050
and then we divided by the height to the power of two.
32
00:02:04,800 --> 00:02:09,060
Now, notice how I've already converted these inputs to floats.
33
00:02:09,090 --> 00:02:13,290
So we don't have to do what we did previously when we were calculating the BMI.
34
00:02:14,940 --> 00:02:17,340
Now what the exercise does mention though
35
00:02:17,370 --> 00:02:20,610
is that we should round the result of the nearest whole number.
36
00:02:21,210 --> 00:02:25,590
But you probably remember how to do this right? Using the round function.
37
00:02:25,830 --> 00:02:29,550
So if we wrap the entire equation inside a round function,
38
00:02:29,880 --> 00:02:32,130
then it's going to round it to the nearest whole number.
39
00:02:32,820 --> 00:02:36,600
So if this is what you got stuck on, then head back to your code and just fix it.
40
00:02:37,260 --> 00:02:40,740
Now, once we've calculated the rounded value of BMI,
41
00:02:41,100 --> 00:02:46,100
the next step is to give the user an interpretation message based on their BMI.
42
00:02:47,070 --> 00:02:48,900
So let's start with the first one.
43
00:02:49,140 --> 00:02:53,100
If the BMI is less than 18.5,
44
00:02:53,520 --> 00:02:56,520
well then in this case, they're actually underweight, right?
45
00:02:56,910 --> 00:03:00,880
So it will print something like your BMI is,
46
00:03:01,480 --> 00:03:04,990
and then inside here we'll insert the value of their BMI.
47
00:03:05,500 --> 00:03:09,420
And this of course is using f-strings, so let's add an F to the beginning.
48
00:03:10,030 --> 00:03:15,030
Your BMI is this particular value and you are underweight.
49
00:03:17,380 --> 00:03:17,860
Now,
50
00:03:17,860 --> 00:03:22,860
what if their BMI was between 18.5 and 25?
51
00:03:23,680 --> 00:03:28,000
Well, because we've already checked that their BMI is less than 18.5,
52
00:03:28,420 --> 00:03:32,950
then if this is false because it's jumped to the next level,
53
00:03:33,280 --> 00:03:34,990
then we don't actually have to check this.
54
00:03:35,290 --> 00:03:39,940
All we have to do is check that it's still below 25. And to do that,
55
00:03:39,940 --> 00:03:41,170
we're going to use an Elif.
56
00:03:41,710 --> 00:03:46,710
So elif BMI is less than 25, well then in this case,
57
00:03:47,260 --> 00:03:51,130
we're going to print you bmi
58
00:03:51,160 --> 00:03:55,810
is this particular value, and you have a normal
59
00:03:55,810 --> 00:04:00,580
weight. If this is a little bit mysterious to you,
60
00:04:00,760 --> 00:04:05,200
why it is that I'm not checking that their BMI is over 18.5,
61
00:04:05,230 --> 00:04:10,120
but below 25 and I'm simply just checking that it's less than 25,
62
00:04:10,390 --> 00:04:13,930
then I recommend having a play around with the code as it is right now,
63
00:04:14,350 --> 00:04:17,350
and trying out some different values for BMI.
64
00:04:17,560 --> 00:04:21,190
Just change this to say 18,
65
00:04:21,190 --> 00:04:26,050
and then try maybe something like 22 so that it's caught by each of these
66
00:04:26,050 --> 00:04:29,950
conditions. And then you'll soon realize that this
67
00:04:30,130 --> 00:04:34,930
if, elif, else is an entire group of statements,
68
00:04:35,320 --> 00:04:39,940
and it's going to check this one first and only if this fails,
69
00:04:39,970 --> 00:04:43,090
when it's false, does it go and check the next one.
70
00:04:43,570 --> 00:04:48,570
So then this is only going to be printed when their BMI is over 18.5,
71
00:04:49,450 --> 00:04:53,170
but below 25. And once you're comfortable with this,
72
00:04:53,230 --> 00:04:55,810
then the rest of the statements should be pretty easy.
73
00:04:58,920 --> 00:04:59,753
All right,
74
00:05:00,030 --> 00:05:00,240
Now,
75
00:05:00,240 --> 00:05:05,240
once we've reached here and their BMI is not less than 35 and we want to catch the
76
00:05:07,650 --> 00:05:10,770
remaining cases where the BMI is over 35,
77
00:05:11,220 --> 00:05:14,160
then all we have to do is add an else statement.
78
00:05:17,520 --> 00:05:18,960
That's all we have to do.
79
00:05:19,380 --> 00:05:23,730
We've managed to check for those people that have BMI less than 18.5.
80
00:05:24,120 --> 00:05:28,230
If it's not less 18.5, well, is it less than 25?
81
00:05:28,650 --> 00:05:33,450
So this will print when the BMI is greater than 18.5,
82
00:05:33,480 --> 00:05:37,920
but less than 25. And we've done that for all of these separate statements.
83
00:05:40,200 --> 00:05:42,150
If you struggled with this code,
84
00:05:42,210 --> 00:05:46,590
then I recommend having a look back at the previous lesson where we talked more
85
00:05:46,590 --> 00:05:50,760
in detail in terms of if, elif and else.
86
00:05:51,810 --> 00:05:53,670
If you struggled with this challenge,
87
00:05:53,730 --> 00:05:58,730
then have a go at pasting the code that's in the solution into the Thonny code
88
00:05:58,760 --> 00:06:03,140
editor. And instead of having the BMI being a workout value,
89
00:06:03,470 --> 00:06:07,640
just set it to some different numbers. So for example, if we start out testing,
90
00:06:07,910 --> 00:06:10,220
if the BMI is equal to 18,
91
00:06:10,550 --> 00:06:15,470
which of these will it fall into, then we can click on the debug step into each
92
00:06:15,470 --> 00:06:18,980
of these. And you can see that when it evaluates this condition,
93
00:06:19,010 --> 00:06:22,100
is the BMI less than 18.5, well,
94
00:06:22,130 --> 00:06:26,300
18 is less than 18.5. So this is true.
95
00:06:26,720 --> 00:06:30,260
And in that case, it's going to execute this line of code.
96
00:06:30,680 --> 00:06:34,220
So it is going to print out your BMI is 18, you are underweight.
97
00:06:34,760 --> 00:06:38,330
And then it's done. That's the end of the code. Now,
98
00:06:38,360 --> 00:06:43,280
if we change this to say 22 however, and we click on debug,
99
00:06:43,940 --> 00:06:48,830
then it's going to evaluate the first condition, is 22 less than 18.5 and
100
00:06:49,070 --> 00:06:50,540
that's going to be false.
101
00:06:50,780 --> 00:06:54,230
So then it skips to the next elif that it can find
102
00:06:54,740 --> 00:06:59,300
and it checks whether if the BMI 22 is less than 25.
103
00:06:59,660 --> 00:07:02,750
So now it's going to execute this print statements.
104
00:07:03,710 --> 00:07:04,970
Have a play around with that
105
00:07:05,150 --> 00:07:08,690
if it's still a little bit mysterious to you and hopefully,
106
00:07:08,690 --> 00:07:09,680
once you're done there,
107
00:07:09,890 --> 00:07:12,800
then you'll be ready to move on to the next code challenge
108
00:07:12,860 --> 00:07:17,150
which is a little bit more challenging. For all of that and more,
109
00:07:17,330 --> 00:07:17,900
I'll see you there.
10422
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.