1
def main():
    name = input("What is your first name?: ")
    name2 = input("What is your last name?: ")
    kg = float(input("What is your weight in kilograms?: "))
    meters = float(input("What is your height in meters?: "))
    mass = float(kg)
    height = float(meters)
    Health(BMI, Ponderal, Rohrer)
    print(name2+",",name,"(BMI:",BMI,",",\
        "Ponderal Index:",Ponderal,",","Rohrer's Index:",Rohrer,",",")")

***It should return something along the lines of Last, First(BMI: 35.234, Ponderal Index: 16.5, Rohrer's Index: 114) Im in a intro to python class and its really late to contact anyone else for help. The whole purpose of this exercise is to create functions and call back to them.

Edit: Thanks a lot for the help guys, I know a lot of the questions on here are normally far more advanced, but the quick replies and the amount of helpful tips is greatly appreciated.

Moose
  • 35
  • 7
  • What version of python are you using? Also you need to indent your code in functions as well. – Zimm3r Oct 12 '13 at 03:21
  • Also it there is no real reason you need the two prints that sandwich your main function call – Zimm3r Oct 12 '13 at 03:24
  • Im using python 3.3.2 – Moose Oct 12 '13 at 03:25
  • To be honest, your code is a mess.. – aIKid Oct 12 '13 at 03:26
  • Ive changed the code around a lot and now its in a working form again, earlier it was returning d, b (BMI: , Ponderal Index: , Rohrer's Index: , ) instead of inputing data into it – Moose Oct 12 '13 at 03:27
  • Like I said, im about 3 weeks into the class, never really touched it before. Im sure it looks horrendous haha – Moose Oct 12 '13 at 03:27
  • keep learning, i guess. see my answer. – aIKid Oct 12 '13 at 03:32
  • I'm no python expert but having a function and a variable with the same name is a bad idea in any language, even if it works. – Tim Oct 12 '13 at 03:34
  • You have posted what your expected results should be, but maybe it would help in the future if you elaborate on the results you are getting. – thtsigma Oct 12 '13 at 03:41
  • Avoid variable names that shadow - especially when they shadow the enclosing function! In this case all the intermediate assignments are useless and should be removed. Unlike VB, one does not need to "assign" a return value to the function - that is what `return` is for. Also, what is `return Health` supposed to do? I think instead, you should return a value based upon the invoked functions .. – user2864740 Oct 12 '13 at 03:47

3 Answers3

1

If a function returns something, then you should put it somewhere. For example, in a variable!

Here, change your functions:

def main():
    name = input("What is your first name?: ")
    name2 = input("What is your last name?: ")
    mass = float(input("What is your weight in kilograms?: "))
    height = float(input("What is your height in meters?: "))
    #mass = float(kg) #not needed
    #height = float(meters) #not needed
    result = health(mass, height)
    #printing based on the return value. result[0] is bmi, and so on.

    print("%s, %s (BMI: %d, Ponderal Index: %d, Rohrer's Index: %d"%(name2,name,health[0],health[1],health[2]))

def bmi (mass, height):
    result = mass / (height ** 2)
    return result

def ponderal (mass, height):
    result = mass / height ** 3
    return result

def rohrer(mass, height):
    result = (mass * 10000) / ((height * 100) ** 3)
    return result

def health (mass, height):
    #calling the functions
    bmi = bmi(mass, height)  #store the returned value to a variable
    ponderal = ponderal(mass, height)
    rohrer = rohrer(mass, height)
    return [bmi,ponderal,rohrer] #return it as a list.

Result:

>>> ================================ RESTART ================================
>>> 
What is your first name?: Akhyar
What is your last name?: Kamili
What is your weight in kilograms?: 50
What is your height in meters?: 1.7
Kamili, Akhyar (BMI: 17.301038062283737 , Ponderal Index: 10.177081213108082 , Rohrer's Index: 0.1017708121310808 , )
>>> 

Some advice:

  1. Do NOT capitalize function names!
  2. Don't name variables like the functions!

Your code will do better.

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • You're an absolute life saver. This helps a lot because I can see the parts where I just screwed the code. – Moose Oct 12 '13 at 03:35
  • How do I change the comma placement? That has been a problem for me the last few assignments. Luckily most of the times it was a string. Id need BMI: 17.301038062283737 , Ponderal Index to turn into BMI: 17.301038062283737, Ponderal Index – Moose Oct 12 '13 at 03:40
  • What do you mean, changing comma placement? – aIKid Oct 12 '13 at 03:41
  • What output do you want? – aIKid Oct 12 '13 at 03:42
  • In the print statement (which is a mess as well), I dont know how to put the comma in behind the variable (name2) so it reads "last, first" instead of "last , first" – Moose Oct 12 '13 at 03:42
  • More about that, see this: http://docs.python.org/2/library/string.html#format-specification-mini-language – aIKid Oct 12 '13 at 03:48
  • (BMI: 30.103806228373706 , Ponderal Index: 17.708121310808064 , Rohrer's Index: 0.17708121310808062 , ) is what it's returning for me. The one you suggested seems a little above what we're learning in class. I also dont really know how to truncate the decimal places so it reads 30.10 for example instead of BMI: 30.103806228373706 – Moose Oct 12 '13 at 03:52
  • It's a little above yeah, usually in the fourth week you learn that. About the decimals, it's more difficult, try looking [here](http://stackoverflow.com/questions/455612/python-limiting-floats-to-two-decimal-points) – aIKid Oct 12 '13 at 03:55
  • Thanks for the help, I think I'll try to submit it with these changes. – Moose Oct 12 '13 at 03:58
  • If this helped you, please accept the answer! (the checklist button next to the answer) :) – aIKid Oct 12 '13 at 04:01
1

There are a lot of problems with your code; first off format it better make sure you have comments on your code (lines that start with #)

Also don't directly convert units from string to float. What if they enter something invalid handle exceptions.

Thirdly the way you format you output text is god awful it is extremely hard to read all the commas and paratheses.

Also the way you get the values you never set them to a variable you also use this health function which you do not need just call the values directly!

Also use sensical names for variables instead of name2 use first name etc

Your code should better look like this (note if this is for homework and you turn this in your professor will easily find it on stackoverflow; so don't)

# calculates the BMI of a person
def BMI (mass, height):
    BMI = mass / (height ** 2)
    return BMI

# calculates the Ponderal index of a person
def Ponderal (mass, height):
    Ponderal = mass / height ** 3
    return Ponderal
# calculates the Rohrer index of a person
def Rohrer (mass, height):
    Rohrer = (mass * 10000) / ((height * 100) ** 3)
    return Rohrer

# this isn't needed
def Health (BMI, Ponderal, Rohrer):
    BMI (mass, height)
    Ponderal (mass, height)
    Rohrer (mass, height)
    return Health


def main():
    # get the names of people
    first_name = input("What is your first name?: ")
    last_name  = input("What is your last name?: ")

    # get their height and weight
    kg     = input("What is your weight in kilograms?: ")
    meters = input("What is your height in meters?: ")

    # convert mass and height to numbers
    try:
        mass = float(kg)
    except ValueError:
        print "Please enter a valid mass."
        return

    try:
        height = float(meters)
    except ValueError:
        print "Please enter a valid height."
        return

    # call the BMI, Ponderal and Rohrer functions
    # don't make the variable BMI as your function is also that name!
    bmi_value = BMI(mass, height)
    ponderal_value = Ponderal(mass, height)
    rohrer_value = Rohrer(mass, height)

    print( "%s, %s (BMI: %s, Ponderal Index: %s, Rohrer Index: %s)" % (last_name, first_name, bmi_value, ponderal_value, rohrer_value) )

    # this print string is EXTREEMLY hard to read
    # print(name2+",",name,"(BMI:",BMI,",", "Ponderal Index:",Ponderal,",","Rohrer's Index:",Rohrer,",",")")

# if we are running this script directly call main
if __name__ == "__main__":
    main()
Zimm3r
  • 3,369
  • 5
  • 35
  • 53
0

You're not calling the functions, you're only referencing them. For example:

Ponderal
# <function Ponderal at blah>

Compared to:

Ponderal()
# A number
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • The code is suppose to just ask the 4 questions, and then compute the 3 calculations, and then finally printing them. the only part of the actual code (not the functions) has to be "main()" – Moose Oct 12 '13 at 03:20