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:02,100
All right. So in this code challenge,
2
00:00:02,160 --> 00:00:06,660
click on the date 2.2 BMI calculator code exercise.
3
00:00:07,320 --> 00:00:11,850
So the goal of the challenge is to be able to work out somebody's body mass
4
00:00:11,850 --> 00:00:15,630
index based on their weight and height that they input.
5
00:00:16,140 --> 00:00:20,790
So the body mass index is basically a way of measuring somebody's body
6
00:00:20,790 --> 00:00:24,420
composition that's kind of independent of their height, right?
7
00:00:24,600 --> 00:00:26,370
If two people are both 60 kilos
8
00:00:26,370 --> 00:00:28,380
but one of them is tall and the other person's short,
9
00:00:28,740 --> 00:00:32,159
then the shorter guy is probably a little bit more overweight, right?
10
00:00:32,610 --> 00:00:36,720
So if you take a look at this chart here, let's say, um, for me,
11
00:00:36,720 --> 00:00:40,620
my height is around 1.8 meters and about 60 something kilos.
12
00:00:40,710 --> 00:00:45,570
So I'm in this normal weight bracket of BMI 18.5 to 25.
13
00:00:46,140 --> 00:00:49,050
So this is a really helpful tool for, um,
14
00:00:49,080 --> 00:00:52,920
healthcare professionals to figure out whether if somebody is underweight,
15
00:00:52,950 --> 00:00:54,180
normal weight or overweight,
16
00:00:54,510 --> 00:00:59,400
that is based on their height and weight. In our program,
17
00:00:59,400 --> 00:01:03,360
we're going to calculate this. So when we run our code,
18
00:01:03,360 --> 00:01:06,390
you can see it's asking us for an input, height in meters.
19
00:01:06,390 --> 00:01:10,950
So mine is 1.8 and my weight is say 63 kilos.
20
00:01:11,430 --> 00:01:12,960
And when I hit enter,
21
00:01:13,050 --> 00:01:18,050
ideally, I would like to get my BMI printed out as a whole number like this.
22
00:01:20,280 --> 00:01:22,650
So how can we achieve this? Well, firstly,
23
00:01:22,650 --> 00:01:25,890
you have to take a look at the equation that's used to calculate the BMI.
24
00:01:26,370 --> 00:01:28,620
It's somebody's weight in kilograms
25
00:01:28,860 --> 00:01:31,380
divided by their height squared.
26
00:01:32,190 --> 00:01:33,960
Then once you've got the result,
27
00:01:33,990 --> 00:01:37,650
we want it to be printed as a whole number or as an integer.
28
00:01:38,130 --> 00:01:41,700
So you'll have to think a little bit about type conversion,
29
00:01:41,730 --> 00:01:45,660
you'll have to think about the mathematical operations that we learned about in
30
00:01:45,660 --> 00:01:49,680
the last lesson and combine everything you've learned so far in order to solve
31
00:01:49,680 --> 00:01:52,860
this code challenge. So pause the video now and give that a go.
32
00:01:52,880 --> 00:01:57,880
[inaudible]
33
00:02:01,280 --> 00:02:02,990
All right. So now that you've had a go at it,
34
00:02:03,050 --> 00:02:06,380
I'm going to reveal of solution. In our code
35
00:02:06,380 --> 00:02:10,759
we've already got access to the height and the weight as variables,
36
00:02:11,180 --> 00:02:14,210
so we can simply use them straight away. So for example,
37
00:02:14,210 --> 00:02:18,710
I could just print the height and it would be equal to whatever the user typed
38
00:02:18,710 --> 00:02:23,660
in when they were asked for it by the input. So in this case,
39
00:02:23,690 --> 00:02:26,060
my height would get printed out as 1.8.
40
00:02:26,780 --> 00:02:28,910
If we can access the height and weight,
41
00:02:29,120 --> 00:02:34,120
then we can calculate the BMI by simply replicating the mathematical equation
42
00:02:34,940 --> 00:02:35,690
here.
43
00:02:35,690 --> 00:02:39,860
So we know that the BMI is equal to the weight divided by the height squared,
44
00:02:40,370 --> 00:02:44,930
so we can take the weight and then we can use the forward-slash for divide.
45
00:02:45,530 --> 00:02:49,700
And then we can divide it by the height squared.
46
00:02:49,790 --> 00:02:53,120
So we're going to use the exponent operator to the power of two.
47
00:02:53,720 --> 00:02:56,090
So this is how we could work out the BMI,
48
00:02:56,690 --> 00:02:59,120
but you'll see that if we actually run this code right now
49
00:02:59,710 --> 00:03:01,090
we'll actually get an error.
50
00:03:01,870 --> 00:03:05,710
And what it's going to tell us is that on line 8,
51
00:03:05,950 --> 00:03:10,480
so right here where our equation is, there's a type error.
52
00:03:11,050 --> 00:03:16,050
And it saying that you're trying to use the exponent or the power function for a
53
00:03:17,080 --> 00:03:19,480
string and an integer.
54
00:03:19,720 --> 00:03:22,150
So the 2 is the integer,
55
00:03:22,750 --> 00:03:27,190
but this is still a string. And we can confirm this
56
00:03:27,190 --> 00:03:30,010
if we check the type. So type of height,
57
00:03:31,100 --> 00:03:35,200
let's comment that out, and let's print this.
58
00:03:35,920 --> 00:03:38,500
Then you'll see that when we enter some numbers here,
59
00:03:38,560 --> 00:03:43,560
the type of height and weight in fact is string because they came from the input
60
00:03:44,110 --> 00:03:44,943
function,
61
00:03:44,980 --> 00:03:49,980
which we saw in previous lessons. Before we can actually just simply run this
62
00:03:50,140 --> 00:03:53,530
equation and get the values we want,
63
00:03:53,590 --> 00:03:57,760
we have to convert all the strings into other data types,
64
00:03:57,850 --> 00:04:02,850
right? Now the weight is probably okay as an integer or a whole number because
65
00:04:04,240 --> 00:04:07,840
it's already pretty accurate, 80 kilos or 85 kilos.
66
00:04:07,960 --> 00:04:10,690
We don't necessarily need any decimal places.
67
00:04:11,170 --> 00:04:16,170
But the height on the other hand definitely needs to be a floating-point number
68
00:04:16,839 --> 00:04:21,279
because if it was an integer, then there would only be height of one meter,
69
00:04:21,339 --> 00:04:23,110
two meter, three meter, four meter.
70
00:04:23,710 --> 00:04:28,450
And because most people lie between one meter and two meter, then we really,
71
00:04:28,450 --> 00:04:31,390
really need those numbers after the decimal place
72
00:04:31,480 --> 00:04:34,300
in order to get an accurate calculation. So the height
73
00:04:34,540 --> 00:04:39,540
definitely needs to be a float and we can convert it into a float using this
74
00:04:39,970 --> 00:04:41,020
line of code here,
75
00:04:41,620 --> 00:04:45,850
where we're converting the string into a float. And the weight
76
00:04:45,880 --> 00:04:50,200
you can either convert it into an integer or a float. It doesn't really matter.
77
00:04:51,070 --> 00:04:55,600
So now if we run our code again, you'll see we no longer have any errors
78
00:04:56,260 --> 00:04:59,740
and we're now able to print our BMI.
79
00:05:00,430 --> 00:05:02,920
Let me enter my height and weight again.
80
00:05:03,250 --> 00:05:07,180
And you can see I'm getting this BMI with a massive list of numbers afterwards.
81
00:05:07,690 --> 00:05:12,160
So in our result, we said it should be a whole number.
82
00:05:12,760 --> 00:05:16,810
How can we convert this BMI into a whole number? Well,
83
00:05:16,840 --> 00:05:21,840
we can turn it into bmi_as_int and convert this floating-point
84
00:05:25,630 --> 00:05:30,630
number, the BMI with all of its numbers after the decimal place, into an integer
85
00:05:31,570 --> 00:05:32,403
a whole number,
86
00:05:32,710 --> 00:05:37,090
which will just simply cut off everything that's after the decimal place.
87
00:05:37,630 --> 00:05:41,290
And now if we print this bmi_as_int,
88
00:05:41,710 --> 00:05:42,700
run our code,
89
00:05:44,860 --> 00:05:48,040
then you'll see that we get a whole number being printed out,
90
00:05:48,520 --> 00:05:52,060
which is now our BMI. In the next lesson
91
00:05:52,060 --> 00:05:56,800
I'm actually going to show you how we can round these floating point numbers so
92
00:05:56,800 --> 00:05:59,750
that when it's 19.5 or above,
93
00:05:59,840 --> 00:06:04,640
it becomes 20 when it's 19.4, 19.3, and then it gets rounded down.
94
00:06:05,150 --> 00:06:07,700
But for now, this is all that we want.
95
00:06:07,730 --> 00:06:12,730
We wanted you to be able to cut off the end and turn it into a whole number.
96
00:06:14,030 --> 00:06:16,550
If you want to have a play around with the solution code,
97
00:06:16,610 --> 00:06:21,610
then head over to this Repl.it over here and you'll be able to see the solution
98
00:06:21,920 --> 00:06:23,600
code with a lot of comments
99
00:06:23,630 --> 00:06:27,710
explaining the code line by line and showing you some different ways of doing
100
00:06:27,710 --> 00:06:29,060
things. For example,
101
00:06:29,300 --> 00:06:34,300
instead of converting the weight and height in line on the line where we're
102
00:06:35,120 --> 00:06:39,350
actually doing the calculation, you could do it beforehand. Alternatively,
103
00:06:39,380 --> 00:06:41,660
instead of using the exponent operator
104
00:06:41,810 --> 00:06:46,010
because something to the power of two is simply just something multiplied by
105
00:06:46,010 --> 00:06:48,200
itself, right? Then in this case,
106
00:06:48,230 --> 00:06:51,080
you could also just do height_as_float times height_as_float.
107
00:06:51,800 --> 00:06:54,680
Have a play around with this code and make sure that you're comfortable with
108
00:06:54,680 --> 00:06:55,580
everything that's here.
109
00:06:55,700 --> 00:06:58,010
And if you got anything wrong at all in the code challenge,
110
00:06:58,130 --> 00:07:00,620
then go back and fix it. Now in the next lesson,
111
00:07:00,680 --> 00:07:03,380
we're going to be talking about more mathematical operators
112
00:07:03,650 --> 00:07:07,250
such as rounding numbers instead of just cutting off the end.
113
00:07:07,700 --> 00:07:10,670
So for all of that and more, I'll see you on the next lesson.
10480
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.