1
00:00:02,340 --> 00:00:03,600
I n this exercise,

2
00:00:03,600 --> 00:00:05,790
we're going to be writing a program

3
00:00:05,790 --> 00:00:09,420
that calculates somebody's Body Mass Index

4
00:00:09,420 --> 00:00:12,210
from a user's weight and height.

5
00:00:12,210 --> 00:00:14,310
So if you look in the description pane,

6
00:00:14,310 --> 00:00:17,850
you'll see a link to the Wikipedia page for BMI.

7
00:00:17,850 --> 00:00:20,550
And if you click on that, you can learn a little bit more

8
00:00:20,550 --> 00:00:24,810
about what the Body mass index is and how it's used.

9
00:00:24,810 --> 00:00:27,150
But essentially, it's a way for us

10
00:00:27,150 --> 00:00:29,220
to measure someone's weight

11
00:00:29,220 --> 00:00:31,200
taking into account their height.

12
00:00:31,200 --> 00:00:33,330
So for example, if a tall person

13
00:00:33,330 --> 00:00:36,180
and a short person both weighed the same,

14
00:00:36,180 --> 00:00:41,180
then in most cases, the shorter person is more overweight.

15
00:00:41,280 --> 00:00:44,190
The formula for calculating BMI

16
00:00:44,190 --> 00:00:48,450
is dividing the person's weight, in kilograms,

17
00:00:48,450 --> 00:00:53,450
by the square of their height, in meters.

18
00:00:53,640 --> 00:00:57,150
This is height multiplied by height,

19
00:00:57,150 --> 00:00:59,460
and then taking that value

20
00:00:59,460 --> 00:01:02,910
and dividing the weight by that value.

21
00:01:02,910 --> 00:01:05,970
Now, if you're not familiar with mathematical notation

22
00:01:05,970 --> 00:01:09,660
for how the BMI is calculated in the formula

23
00:01:09,660 --> 00:01:13,560
in the description pane, then feel free to look up in Google

24
00:01:13,560 --> 00:01:17,310
for alternative ways of representing this formula.

25
00:01:17,310 --> 00:01:18,840
But it's very important

26
00:01:18,840 --> 00:01:21,510
that the inputs are in the correct units.

27
00:01:21,510 --> 00:01:25,980
So in this case, it's metric units, kilograms and meters.

28
00:01:25,980 --> 00:01:30,210
And the goal is to be able to print out the BMI

29
00:01:30,210 --> 00:01:32,850
using any potential inputs,

30
00:01:32,850 --> 00:01:34,890
where the first input is the height

31
00:01:34,890 --> 00:01:37,140
and the second input is the weight,

32
00:01:37,140 --> 00:01:40,200
in meters and in kilograms.

33
00:01:40,200 --> 00:01:41,370
Now, one thing to note

34
00:01:41,370 --> 00:01:44,910
is that in our tests for this exercise,

35
00:01:44,910 --> 00:01:49,910
we're expecting the BMI to be printed out as a whole number.

36
00:01:49,920 --> 00:01:51,720
So think about what you need to do

37
00:01:51,720 --> 00:01:54,000
in order to make that happen.

38
00:01:54,000 --> 00:01:56,340
Otherwise, when you click SUBMIT,

39
00:01:56,340 --> 00:02:00,270
your answer might not match the expected output.

40
00:02:00,270 --> 00:02:02,880
See if you can create this program.

41
00:02:02,880 --> 00:02:07,170
You can always Google for a Body Mass Index Calculator

42
00:02:07,170 --> 00:02:10,410
and check your result against their result

43
00:02:10,410 --> 00:02:12,330
to figure out if you went wrong anywhere

44
00:02:12,330 --> 00:02:14,760
or if you need to change your code.

45
00:02:14,760 --> 00:02:16,590
So good luck, and give that a go.

46
00:02:16,590 --> 00:02:19,440
We'll go through the solution together on the next slide.

47
00:02:23,280 --> 00:02:28,080
Now, the first thing we want to do is to convert the numbers

48
00:02:28,080 --> 00:02:30,570
that we're getting through the inputs

49
00:02:30,570 --> 00:02:33,480
into numbers that we can work with

50
00:02:33,480 --> 00:02:36,360
and do mathematical operations on.

51
00:02:36,360 --> 00:02:38,700
So that means on line 4, you'll see

52
00:02:38,700 --> 00:02:42,780
that I've created the weight_as_integer variable

53
00:02:42,780 --> 00:02:47,780
to store the weight input as a whole number.

54
00:02:48,240 --> 00:02:52,800
So I'm converting, in this case, in the example input 69

55
00:02:52,800 --> 00:02:57,800
into a number that I can multiply or add and use later on.

56
00:02:59,250 --> 00:03:03,990
On line 5, I'm converting the height that is coming in

57
00:03:03,990 --> 00:03:05,820
with height in meters.

58
00:03:05,820 --> 00:03:10,110
And because none of us have one meter or two meter,

59
00:03:10,110 --> 00:03:12,240
we're usually somewhere in between the two.

60
00:03:12,240 --> 00:03:15,990
This is usually represented as a floating point number.

61
00:03:15,990 --> 00:03:18,480
So there's a decimal point in there.

62
00:03:18,480 --> 00:03:22,080
For example, I am 1 meter 79,

63
00:03:22,080 --> 00:03:25,980
and that will be 1.79 meters.

64
00:03:25,980 --> 00:03:28,260
In this case, we're going convert that input

65
00:03:28,260 --> 00:03:30,000
into a different data type,

66
00:03:30,000 --> 00:03:32,790
one that can store decimal places,

67
00:03:32,790 --> 00:03:35,400
and that is the "float data type".

68
00:03:35,400 --> 00:03:39,540
So we wrap the float around the height variable,

69
00:03:39,540 --> 00:03:43,920
and we can turn that input 1.63 in the sample input

70
00:03:43,920 --> 00:03:47,190
into a floating point number,

71
00:03:47,190 --> 00:03:51,060
which again can be used in our calculations later on.

72
00:03:51,060 --> 00:03:55,800
We can calculate the BMI using various methods.

73
00:03:55,800 --> 00:03:58,560
One method and probably the shortest method,

74
00:03:58,560 --> 00:04:01,590
but this would involve a little bit of Googling around,

75
00:04:01,590 --> 00:04:06,590
is to figure out how to use the exponent operator (**) in Python.

76
00:04:07,140 --> 00:04:08,880
Now, we haven't talked about this,

77
00:04:08,880 --> 00:04:11,730
but it's good to get used to the idea

78
00:04:11,730 --> 00:04:14,790
that there are multiple ways of doing everything.

79
00:04:14,790 --> 00:04:16,620
And just because you did it differently

80
00:04:16,620 --> 00:04:18,570
doesn't mean it's wrong.

81
00:04:18,570 --> 00:04:21,930
So I wanted to present you with many ways.

82
00:04:21,930 --> 00:04:26,930
Now, the exponent operator is equivalent to a math

83
00:04:27,270 --> 00:04:29,310
where you see that super scripted number,

84
00:04:29,310 --> 00:04:31,740
so the little number at the top.

85
00:04:31,740 --> 00:04:36,360
In this case, we need to square the height,

86
00:04:36,360 --> 00:04:39,540
so that means multiplying the height by itself,

87
00:04:39,540 --> 00:04:42,420
or in math, this is called raising the height

88
00:04:42,420 --> 00:04:44,670
to a power of two.

89
00:04:44,670 --> 00:04:45,960
And we can do that in Python

90
00:04:45,960 --> 00:04:48,360
using the double asterisk operator.

91
00:04:48,360 --> 00:04:50,880
So height as a float, two asterisks,

92
00:04:50,880 --> 00:04:55,410
and then the number two (height_as_float **2) will square the height.

93
00:04:55,410 --> 00:04:56,730
And then all we have to do

94
00:04:56,730 --> 00:04:59,370
is do weight divided by height squared,

95
00:04:59,370 --> 00:05:01,113
and we end up with our BMI.

96
00:05:03,270 --> 00:05:08,130
An alternative way of doing this is by using parentheses.

97
00:05:08,130 --> 00:05:10,200
You might remember from high school maths,

98
00:05:10,200 --> 00:05:12,240
in the US it's called PEDMAS

99
00:05:12,240 --> 00:05:14,910
and in the UK it's called BODMAS,

100
00:05:14,910 --> 00:05:19,170
but brackets always come before any other operation.

101
00:05:19,170 --> 00:05:21,510
In this case, we can't simply just write

102
00:05:21,510 --> 00:05:23,610
weight divided by height times height,

103
00:05:23,610 --> 00:05:26,460
because the first operation that's going to be calculated

104
00:05:26,460 --> 00:05:28,140
is weight divided by height,

105
00:05:28,140 --> 00:05:30,930
and it's going to give us erroneous answers.

106
00:05:30,930 --> 00:05:34,860
But by wrapping height times height in a set of parentheses,

107
00:05:34,860 --> 00:05:39,030
we ensure that part of the operation gets carried out first,

108
00:05:39,030 --> 00:05:40,650
and then we take the result

109
00:05:40,650 --> 00:05:43,800
and then we divide weight by the result.

110
00:05:43,800 --> 00:05:48,180
This is more likely the way that you might have solved this,

111
00:05:48,180 --> 00:05:49,770
but you might have had an error

112
00:05:49,770 --> 00:05:52,590
because you forgot about the parentheses.

113
00:05:52,590 --> 00:05:54,663
So this is something to note.

114
00:05:55,620 --> 00:05:59,670
Finally, we can convert the BMI into a whole number,

115
00:05:59,670 --> 00:06:02,490
so rounding it if necessary,

116
00:06:02,490 --> 00:06:06,120
and then we print it out as the solution.

117
00:06:06,120 --> 00:06:09,390
And this will allow us to pass the tests

118
00:06:09,390 --> 00:06:11,073
in the previous slide.

