Hi,

I set out to make a body fat calculator tonight. There are a number of things that need to be improved in the below code (any suggestions welcome) but there's one thing I am really baffled by. I get a syntax error on the last few print lines. Please see the code and the traceback.

``````import math

# the formulas for calculation are taken from http://www.topendsports.com/testing/bodyfat-girths.htm
print "This is a programme to calculate your body fat using girth measurements"
print "This is from the metric version of the Navy body fat formula"

m_f = raw_input("Are you male or female (enter m or f)?: ")

if m_f == "m":
print
print"I'll use the formula to calculate the body fat of a male"
print

if m_f == "f":
print
print "I'll use the formula to calculate the body fat of a female. \nI'll need to know your hip measurement too."
print

height = raw_input("What is your height (in cm)?: ")
waist = raw_input("What is your waist girth (in cm)?: ")
neck = raw_input("What is your neck girth (in cm)?: ")
bodyweight = raw_input("What is your bodyweight (in kg)?: ")

if m_f == "f":
hip = raw_input("What is your hip girth (in cm)?: ")
hip = int(hip)

bodyweight= int(bodyweight)
waist = int(waist)
neck = int(neck)
height = int(height)

if m_f == "m":
body_fat = 495/(1.0324-0.19077*(math.log10(waist-neck))+0.15456*(math.log10(height)))-450

if m_f == "f":
body_fat = 495/(1.29579-0.35004*(math.log10(waist+hip-neck))+0.22100*(math.log10(height)))-450

fat_kg = body_fat*bodyweight
lbw = bodyweight-fat_kg
bmi = bodyweight/(math.pow((height/100), 2)

print 'The following are approximations.'
print '\n \n'
print 'Your lean body weight is %ikg' % lbw
print 'You have %ikg of fat on your body' % fat_kg
print '\n \n'
print "Your body fat percentage is %i" % body_fat
print "\n \n'
print "Your body mass index (BMI) is %i" % bmi``````

Traceback:

``````compile:
invalid syntax (bodyfat calculator.py, line 51)

Traceback (most recent call last):
File "/usr/share/drpython/drpython/drpython.py", line 670, in CheckSyntax
compile(ctext, fn, 'exec')
<type 'exceptions.SyntaxError'>: invalid syntax (bodyfat calculator.py, line 51)``````

## All 4 Replies

There is a missing closing parenthesis at line 46 !

THANKS!

This is the new tidied up code:

``````import math

# the formulas for calculation are taken from http://www.topendsports.com/testing/bodyfat-girths.htm
print "This is a programme to calculate your body fat using girth measurements"
print "This is from the metric version of the Navy body fat formula"

m_f = raw_input("Are you male or female (enter m or f)?: ")

if m_f == "m":
print
print"I'll use the formula to calculate the body fat of a male"
print

if m_f == "f":
print
print "I'll use the formula to calculate the body fat of a female. \nI'll need to know your hip measurement too."
print

height = raw_input("What is your height (in cm)?: ")
waist = raw_input("What is your waist girth (in cm)?: ")
neck = raw_input("What is your neck girth (in cm)?: ")
bodyweight = raw_input("What is your bodyweight (in kg)?: ")

if m_f == "f":
hip = raw_input("What is your hip girth (in cm)?: ")
hip = int(hip)

bodyweight= int(bodyweight)
waist = int(waist)
neck = int(neck)
height = int(height)

if m_f == "m":
body_fat = 495/(1.0324-0.19077*(math.log10(waist-neck))+0.15456*(math.log10(height)))-450

if m_f == "f":
body_fat = 495/(1.29579-0.35004*(math.log10(waist+hip-neck))+0.22100*(math.log10(height)))-450

fat_kg = body_fat*bodyweight
lbw = bodyweight-fat_kg
bmi = bodyweight/(math.pow((height/100), 2))

print 'The following are approximations.'
print '\n \n'
print 'Your lean body weight is %ikg' % lbw
print 'You have %ikg of fat on your body' % fat_kg
print '\n \n'
print "Your body fat percentage is %i" % body_fat
print '\n \n'
print "Your body mass index (BMI) is %i" % bmi``````

Now I get

``````Traceback (most recent call last):
File "/home/james/python shit/bodyfat calculator.py", line 46, in <module>
bmi = bodyweight/(math.pow((height/100), 2))
ZeroDivisionError: float division``````

But I'll try to work that out myself.

Now I get

``````Traceback (most recent call last):
File "/home/james/python shit/bodyfat calculator.py", line 46, in <module>
bmi = bodyweight/(math.pow((height/100), 2))
ZeroDivisionError: float division``````

But I'll try to work that out myself.