0

I am trying to make a BMI calculator function. I am learning python at pyschools. This is my code:

# Note: Return a string of 1 decimal place.
def BMI(weight, height): 
    x = weight /(height*height)
    g = round(x,1)
    return g

And pyschools shows me that these are the right answers: With 110 = weight and 2 = height I am supposed to get a BMI of 27,5. But I instead get 27.

Then it does a second check to make sure I wrote the code right and tells me 24,2 is the right answer but my program did return 24,2. But it still marks my answer in red and says "my" 24,2 is wrong and the website's is right.

If someone has a better site or anything to learn python it would also be appreciated since this website seems to be kind of wrong sometimes. And I am looking for free online resources. No books please.

agconti
  • 17,780
  • 15
  • 80
  • 114
Czarek
  • 597
  • 1
  • 10
  • 20
  • 2
    Python 2, right? Then you are using integer division. – Martijn Pieters Apr 16 '14 at 13:12
  • Actually, given that height is a decimal (you calculate BMI with height in meters, not in centimeters) this _should_ already use float division - but only if you explicitly pass float parameters, i.e. you have to pass 2.0 instead of 2. – tobias_k Apr 16 '14 at 13:16
  • @MartijnPieters Its python 3 I believe. – Czarek Apr 16 '14 at 13:16
  • @czaarek99: no, because your calculator works fine in Python 3. If you are supposed to use Python 3, you're using the wrong version, I am afraid. – Martijn Pieters Apr 16 '14 at 13:17
  • @czaarek99 check your Python version as `>>> import sys` then `>>> sys.version` – Grijesh Chauhan Apr 16 '14 at 13:17
  • @GrijeshChauhan The thing is I am learning at pyschools. And there is a window you put the code into and it compiles and then checks if you did good. – Czarek Apr 16 '14 at 13:20
  • @czaarek99: Pyschools uses Python 2 (2.7 to be exact, it runs on the Google App Engine). – Martijn Pieters Apr 16 '14 at 13:21
  • @MartijnPieters Anything like that with python 3 then? Since I can't learn anything by just reading the python docs. – Czarek Apr 16 '14 at 13:23
  • @czaarek99: the python documentation includes a tutorial. There are plenty more [listed on the Python wiki](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers). – Martijn Pieters Apr 16 '14 at 13:24
  • @czaarek99 There aren't many online Python 3 interpreters out there, but if you look at my answer you can easily make that exercise code work on Python 2. – anon582847382 Apr 16 '14 at 13:27

3 Answers3

1

To fix it for all cases, add this line to the top:

from __future__ import division  # Make division work like in Python 3.

in Python 2, / means integer division.


With this in mind, in Python 2 if you pass intgers into division, it will give you an integer back. Anything that would have been a float is floored*. Therefore another option to get the desired result is to pass a float in, so instead of:

weight / (height*height)

do:

float(weight) / (height*height)  # float in means float out.

*This means that only full times that the divisor goes in are counted. So 1/2 will get 0 because 2 goes fully into 1 0 times.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
0
def BMI(weight, height): 
    x = float(weight) /(height*height)
    g = round(x,1)
    return g

see Python division

and Binary arithmetic operations

Community
  • 1
  • 1
laike9m
  • 18,344
  • 20
  • 107
  • 140
0

The issue lies with your division.

Division as we intrinsically know it is floating point division, or division where 1 / 2 evaluates to a fraction, 0.5. In standard programatic division, the 1, 2 are ints() and therefore cant be fractions, or floats() as the type is called in python. The expression, 1 / 2 therefore evaluates as 0, as 2 as a whole integer cant go into one entirely any times.

Ex:

In [1]: 1 / 2
Out[1]: 0

# Explicitly what is going on, since 1 and 2 are ints. 
In [2]: int(1) / int(2)
Out[2]: 0

#fixed with floating division
In [3]: float(1) / float(2)
Out[3]: 0.5

# protip: only one of the divisors needs to be a float for python to divide correctly.
In [4]: 1 / float(2)
Out[4]: 0.5

Use x = weight / float((height*height)) to get the results you expect.

# Note: Return a string of 1 decimal place.
def BMI(weight, height): 
    x = weight / float((height*height))
    g = round(x,1)
    return g
agconti
  • 17,780
  • 15
  • 80
  • 114