height = float(input("What is your height in meters?"))
weight = float(input("What is your weight in kg?"))
sum = weight/height/height
print('The sum is {0} divided by {1} divided by {1} equals {2}'.format(weight,height,sum))

- 21,021
- 5
- 43
- 58
-
I assume that you read the [documentation for `round`](https://docs.python.org/3/library/functions.html#round). Which part of it do you have problems with? – Matthias Jun 18 '17 at 15:40
-
3Possible duplicate of [Python - how to round down to 2 decimals](https://stackoverflow.com/questions/20457038/python-how-to-round-down-to-2-decimals) – Arya McCarthy Jun 18 '17 at 15:48
-
2@Coldspeed: Was that at all helpful? – Bill Bell Jun 18 '17 at 15:56
1 Answers
Life will be easier for you, as a programmer, if you use google. And I mean no offence. In this case, just query for python round.
In this particular case, if you want to round a float called BMD
to two decimal places then you would use the following expression,
round(BMD,2)
You might also be interested in how to use formatting to achieve a similar result. First I deliberately set BMD
to a value that has only two decimal places. I can print it as-is but I have no control. In the second case, I set BMD
to a value for quite a few decimal places but this time I limit the number of output decimal places.
In my opinion, it's safer to use round
and then use the first formatting option because the second option could stumble with a value such as 345.22 (which occupies more than four columns overall).
>>> BMD = 5.21
>>> print ('BMD is {}'.format(BMD))
BMD is 5.21
>>> BMD = 5.213333333333333
>>> print ('BMD is {:4.2f}'.format(BMD))
BMD is 5.21

- 21,021
- 5
- 43
- 58