[Tutor] BMI calc

Altay Aksulu altay at sympatico.ca
Wed Mar 13 12:48:59 CET 2013


Assuming the BMI formula is correct, the following is how I would have done it in Python 3. You can safely ignore the comments. If you want to run it, copy and paste the code into a text editor (you can delete the #comments), save it with a .py extension (e.g. bmi.py) and run it from within a terminal screen by typing at the shell prompt python3 bmi.py (in Windows command line you may not need the 3, I am not sure). If you need further conversions (e.g. converting height from ft-in to in or from ft-in to cms and so on) you can add a few more lines of code to do that.

def main():                                                                          #it is good practice to modularize your program. Hence, this starts with a main() function. Defined here and called in the last line.     w=float(input('Please enter your weight in pounds: '))            #You need to capture weight and height. input() built-in function is used to get the value that will be referenced by the w variable.    h=float(input('Please enter your height in inches: '))              # Since input() always returns string type, float() -another built in function- is used to convert it to a real number.    calc_BMI(w,h)                                                                 #Here you call the BMI calculation function you wrote. Note that you are passing w and h values into this function.
def calc_BMI(weight,height):                                                  #w and h will be assigned to weight and height -in that order- as parameter values.    bmi=(weight/(height*height))*703.0                                  #actual calculation. the result is referenced by the bmi variable.    if bmi<18.5:                                                                    #removed the equal sign here. In your version 18.5 would have fallen into two categories.        print('underweight')    elif bmi>= 18.5 and bmi <=24.9:        print('normal weight')    elif bmi >=25 and bmi <=29.9:        print('overweight')    else:        print('obese')
main()
Haven't tried but it should work.
Altay Aksulu.





From: tutor-request at python.org
Subject: Tutor Digest, Vol 109, Issue 39
To: tutor at python.org
Date: Wed, 13 Mar 2013 11:44:10 +0100

Send Tutor mailing list submissions to
	tutor at python.org
 
To subscribe or unsubscribe via the World Wide Web, visit
	http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
	tutor-request at python.org
 
You can reach the person managing the list at
	tutor-owner at python.org
 
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Tutor digest..."


--Forwarded Message Attachment--
From: ysoliman at uncc.edu
To: breamoreboy at yahoo.co.uk; tutor at python.org
Date: Wed, 13 Mar 2013 03:07:27 +0000
Subject: Re: [Tutor] BMI calc

I'm using python 2.7, where exactly does the return statment with the (return (weight/(height*height))*703 calculation get posted in the function? 
 
def calc_BMI(weight,height):
    if bmi <=18.5:
        return 'underweight'
    elif bmi >= 18.5 and bmi <=24.9:
        return 'normal weight'
    elif bmi >=25 and bmi <=29.9:
            return 'overweight'
    elif bmi >=30:
        return 'obese'   
 
The user will need to input height ex) 5'3 and which will need to be split and stripped in order to convert to inches.
________________________________________
From: Tutor [tutor-bounces+ysoliman=uncc.edu at python.org] on behalf of Mark Lawrence [breamoreboy at yahoo.co.uk]
Sent: Tuesday, March 12, 2013 10:29 PM
To: tutor at python.org
Subject: Re: [Tutor] BMI calc
 
On 13/03/2013 02:08, Dave Angel wrote:
> On 03/12/2013 09:46 PM, Mark Lawrence wrote:
>> On 13/03/2013 00:05, Soliman, Yasmin wrote:
>>> Hello all,
>>>
>>> I'm new to python and its been a stuggle so far. I'm attempting to
>>> create a BMI calculator in Wing 101 4.1. I keep getting syntax errors:
>>>
>>> def calc_BMI(weight,height):
>>>      return (weight/(height*height))*703.0
>>> if bmi <=18.5:
>>>      print 'underweight'
>>> elif bmi >= 18.5 and bmi <=24.9:
>>>          print 'normal weight'
>>> elif bmi >=25 and bmi <=29.9:
>>>              print 'overweight'
>>> elif bmi >=30:
>>>                  print 'obese'
>>>
>>> Also, height should be converted to inches and I have not the
>>> slightest clue how to so. Any help would be much appreciated.
>>> _
>>>
>>
>> If you're using Python 3 you'll get syntax errors as print is a
>> function, not a statement as in Python 2.  Is this your problem?  If not
>> please cut and paste the exact syntax error for us to see.  Then we'll
>> sort the rest of the problems :)
>>
>
> As Mark implies, tell us exactly what Python version you're using, and
> copy/paste the entire error message, including traceback.
>
> For example, when I run it on 3.3, I have:
>
> davea at think2:~/temppython$ python3.3 soliman.py
>    File "soliman.py", line 6
>      print 'underweight'
>                        ^
> SyntaxError: invalid syntax
>
>
> Also, could you tell us your experience level?  You've defined a
> function, but you haven't called it.  And yet you're apparently trying
> to use the results of calling it.
>
> As for converting height to inches, that depends on what you started
> with.  Since you're using the English version of the formula, you should
> already have inches and pounds before calling that function.
>
> So perhaps in addition to missing the code for inputting the values
> (perhaps the raw_input function), you're also missing the conversion
> from "something" to inches.  How are you getting the raw numbers, and
> what units are they in?
>
> If the raw numbers are in meters and kg, then you'd want to remove the
> multiply by 703.  Or you could convert from meters to inches by
> multiplying by 39.?  (look up the exact number).  And from kg to pounds
> by a similar multiply by something like 2.2
>
 
Of course if you've got the wrong algorithm you're wasting your time
anyway, see for example
http://www.newscientist.com/article/mg20928030.200-obesity-expert-a-better-fat-measure-than-bmi.html
:)
 
--
Cheers.
 
Mark Lawrence
 
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
 


--Forwarded Message Attachment--
From: joskerc at gmail.com
CC: breamoreboy at yahoo.co.uk; tutor at python.org
To: ysoliman at uncc.edu
Date: Wed, 13 Mar 2013 07:17:47 +0100
Subject: Re: [Tutor] BMI calc

Hi,


On Wed, Mar 13, 2013 at 4:07 AM, Soliman, Yasmin <ysoliman at uncc.edu> wrote:

I'm using python 2.7, where exactly does the return statment with the (return (weight/(height*height))*703 calculation get posted in the function?




def calc_BMI(weight,height):

    if bmi <=18.5:

        return 'underweight'

    elif bmi >= 18.5 and bmi <=24.9:

        return 'normal weight'

    elif bmi >=25 and bmi <=29.9:

Following return is out of alignment. Should be at the same place in the line as the oters. 

            return 'overweight'

    elif bmi >=30:

        return 'obese'



The user will need to input height ex) 5'3 and which will need to be split and stripped in order to convert to inches.

Exactly. Do you have a problem with this? If so what?

Dreetz




--Forwarded Message Attachment--
From: davea at davea.name
To: tutor at python.org
Date: Wed, 13 Mar 2013 03:10:40 -0400
Subject: Re: [Tutor] BMI calc

On 03/13/2013 02:17 AM, Jos Kerc wrote:
> Hi,
>
>
> On Wed, Mar 13, 2013 at 4:07 AM, Soliman, Yasmin <ysoliman at uncc.edu> wrote:
>
>>   <SNIP>
>>
>> def calc_BMI(weight,height):
>>      if bmi <=18.5:
>>          return 'underweight'
>>      elif bmi >= 18.5 and bmi <=24.9:
>>          return 'normal weight'
>>      elif bmi >=25 and bmi <=29.9:
>>
>
> Following return is out of alignment. Should be at the same place in the
> line as the oters.
>
>
>>              return 'overweight'
 
It would certainly look prettier, but it's not a language requirement. 
Only within a single block do the columns have to line up exactly.
 
 > <SNIP>
 
-- 
DaveA
 


--Forwarded Message Attachment--
From: alan.gauld at btinternet.com
To: tutor at python.org
Date: Wed, 13 Mar 2013 08:36:05 +0000
Subject: Re: [Tutor] BMI calc

On 13/03/13 03:07, Soliman, Yasmin wrote:
> I'm using python 2.7,
 
Then let us see the syntax errors because otherwise we are guessing.
 
 > where exactly does the return statment ... get posted in the function?
 
I'm not sure what you mean by 'get posted'
It is activated once the function is called.
 
ie
 
def foo(x):
    return 2 + x
 
does nothing until I call it:
 
result = foo(5)
print result
 
> def calc_BMI(weight,height):
>      if bmi <=18.5:
>          return 'underweight'
 
Since BMI is a value your function is better as it was, just returning 
the value.
 
The code to convert that BMI value into a weight assessment is better 
done outside, again as you had before. (Or inside another function, 
rateBMI(bmi) maybe)
 
What was missing was some glue that joins those 2 things together:
 
read weight and height
call CalcBMI
use result to assess weight.
 
>      elif bmi >= 18.5 and bmi <=24.9:
>          return 'normal weight'
 
BTW in Python you can abbreviate the tests slightly with
 
elif 18.5 <= bmi <= 24.9:
    print 'normal weight'
 
HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
 
 


--Forwarded Message Attachment--
From: sydney.shall at kcl.ac.uk
To: tutor at python.org
Date: Wed, 13 Mar 2013 10:44:02 +0000
Subject: Re: [Tutor] BMI calc


  
    
  
  
    On 13/03/2013 00:05, Soliman, Yasmin
      wrote:

    
    
      Hello all,

I'm new to python and its been a stuggle so far. I'm attempting to create a BMI calculator in Wing 101 4.1. I keep getting syntax errors:

def calc_BMI(weight,height):
    return (weight/(height*height))*703.0
if bmi <=18.5:
    print 'underweight'
elif bmi >= 18.5 and bmi <=24.9:
        print 'normal weight'
elif bmi >=25 and bmi <=29.9:
            print 'overweight'
elif bmi >=30:
                print 'obese'

Also, height should be converted to inches and I have not the slightest clue how to so. Any help would be much appreciated. 
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


    
    Hi,

      I am also a newbie, but I wish to learn, so I
        offer the following corrections.

        

      # Note. I do note know what units you are using.

    # I would use scientific, metric units. So you need to divide both
    weight and height by a suitable conversion factors.

    # Then you will not need the number 703.0, and the units in the
    second and third lines would be Kg/m. [Kilogram/Metre) 

    def calc_BMI(weight,height):
    bmi = (weight/(height*height))*703.0
    print 'Your BMI is : ', BMI 'weight units/height units.' # You need to put the correct text here.
    if bmi <=18.5:
        print 'underweight'
    if  18.5 <= bmi <= 24.9:
        print 'normal weight'
    if  25.0 <= bmi <= 29.9:
        print 'overweight'
    if bmi >= 30:
        print 'obese'
    return

    I hope that I am halfway correct.

        With best wishes,

          Sydney

        

      

    -- 
Professor Sydney Shall,
Department of Haematological Medicine,
King's College London,
Medical School,
123 Coldharbour Lane,
LONDON SE5 9NU,
Tel & Fax: +44 (0)207 848 5902,
E-Mail: sydney.shall,
[correspondents outside the College should add; @kcl.ac.uk]
www.kcl.ac.uk 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130313/9b3c7bae/attachment-0001.html>


More information about the Tutor mailing list