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
All right. So in this code challenge,
click on the date 2.2 BMI calculator code exercise.
So the goal of the challenge is to be able to work out somebody's body mass
index based on their weight and height that they input.
So the body mass index is basically a way of measuring somebody's body
composition that's kind of independent of their height, right?
If two people are both 60 kilos
but one of them is tall and the other person's short,
then the shorter guy is probably a little bit more overweight, right?
So if you take a look at this chart here, let's say, um, for me,
my height is around 1.8 meters and about 60 something kilos.
So I'm in this normal weight bracket of BMI 18.5 to 25.
So this is a really helpful tool for, um,
healthcare professionals to figure out whether if somebody is underweight,
normal weight or overweight,
that is based on their height and weight. In our program,
we're going to calculate this. So when we run our code,
you can see it's asking us for an input, height in meters.
So mine is 1.8 and my weight is say 63 kilos.
And when I hit enter,
ideally, I would like to get my BMI printed out as a whole number like this.
So how can we achieve this? Well, firstly,
you have to take a look at the equation that's used to calculate the BMI.
It's somebody's weight in kilograms
divided by their height squared.
Then once you've got the result,
we want it to be printed as a whole number or as an integer.
So you'll have to think a little bit about type conversion,
you'll have to think about the mathematical operations that we learned about in
the last lesson and combine everything you've learned so far in order to solve
this code challenge. So pause the video now and give that a go.
[inaudible]
All right. So now that you've had a go at it,
I'm going to reveal of solution. In our code
we've already got access to the height and the weight as variables,
so we can simply use them straight away. So for example,
I could just print the height and it would be equal to whatever the user typed
in when they were asked for it by the input. So in this case,
my height would get printed out as 1.8.
If we can access the height and weight,
then we can calculate the BMI by simply replicating the mathematical equation
here.
So we know that the BMI is equal to the weight divided by the height squared,
so we can take the weight and then we can use the forward-slash for divide.
And then we can divide it by the height squared.
So we're going to use the exponent operator to the power of two.
So this is how we could work out the BMI,
but you'll see that if we actually run this code right now
we'll actually get an error.
And what it's going to tell us is that on line 8,
so right here where our equation is, there's a type error.
And it saying that you're trying to use the exponent or the power function for a
string and an integer.
So the 2 is the integer,
but this is still a string. And we can confirm this
if we check the type. So type of height,
let's comment that out, and let's print this.
Then you'll see that when we enter some numbers here,
the type of height and weight in fact is string because they came from the input
function,
which we saw in previous lessons. Before we can actually just simply run this
equation and get the values we want,
we have to convert all the strings into other data types,
right? Now the weight is probably okay as an integer or a whole number because
it's already pretty accurate, 80 kilos or 85 kilos.
We don't necessarily need any decimal places.
But the height on the other hand definitely needs to be a floating-point number
because if it was an integer, then there would only be height of one meter,
two meter, three meter, four meter.
And because most people lie between one meter and two meter, then we really,
really need those numbers after the decimal place
in order to get an accurate calculation. So the height
definitely needs to be a float and we can convert it into a float using this
line of code here,
where we're converting the string into a float. And the weight
you can either convert it into an integer or a float. It doesn't really matter.
So now if we run our code again, you'll see we no longer have any errors
and we're now able to print our BMI.
Let me enter my height and weight again.
And you can see I'm getting this BMI with a massive list of numbers afterwards.
So in our result, we said it should be a whole number.
How can we convert this BMI into a whole number? Well,
we can turn it into bmi_as_int and convert this floating-point
number, the BMI with all of its numbers after the decimal place, into an integer
a whole number,
which will just simply cut off everything that's after the decimal place.
And now if we print this bmi_as_int,
run our code,
then you'll see that we get a whole number being printed out,
which is now our BMI. In the next lesson
I'm actually going to show you how we can round these floating point numbers so
that when it's 19.5 or above,
it becomes 20 when it's 19.4, 19.3, and then it gets rounded down.
But for now, this is all that we want.
We wanted you to be able to cut off the end and turn it into a whole number.
If you want to have a play around with the solution code,
then head over to this Repl.it over here and you'll be able to see the solution
code with a lot of comments
explaining the code line by line and showing you some different ways of doing
things. For example,
instead of converting the weight and height in line on the line where we're
actually doing the calculation, you could do it beforehand. Alternatively,
instead of using the exponent operator
because something to the power of two is simply just something multiplied by
itself, right? Then in this case,
you could also just do height_as_float times height_as_float.
Have a play around with this code and make sure that you're comfortable with
everything that's here.
And if you got anything wrong at all in the code challenge,
then go back and fix it. Now in the next lesson,
we're going to be talking about more mathematical operators
such as rounding numbers instead of just cutting off the end.
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.