为什么我的代码不起作用?

时间:2013-10-12 03:16:31

标签: python function

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,",",")")

***它应该返回一些东西 最后,第一(BMI:35.234,Ponderal指数:16.5,Rohrer指数:114) 我正在介绍python类,它很晚才能联系其他人寻求帮助。本练习的目的是创建函数并回调它们。

编辑:非常感谢帮助人员,我知道这里的很多问题通常都要先进得多,但我们非常感谢快速回复和大量有用的提示。< / p>

3 个答案:

答案 0 :(得分:1)

如果函数返回某些内容,那么你应该将它放在某个地方。例如,在变量中!

在此处,更改您的功能:

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.

结果:

>>> ================================ 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 , )
>>> 

一些建议:

  1. 不要大写函数名称!
  2. 不要将变量命名为函数!
  3. 您的代码会做得更好。

答案 1 :(得分:1)

您的代码存在很多问题;首先关闭格式,最好确保您对代码有意见(以#开头的行)

也不要直接将单位从字符串转换为浮点数。如果他们输入无效的句柄异常会怎样。

第三,你设置输出文本格式的方式很糟糕,很难阅读所有的逗号和副词。

另外,您获取值的方式从未将它们设置为变量,您也可以使用此健康功能,您不需要直接调用值!

还使用变量的敏感名称而不是name2使用名字等

你的代码最好看起来像这样(注意这是否适用于家庭作业而你在教授中转向轻松在stackoverflow上找到它;所以不要这样做)

# 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()

答案 2 :(得分:0)

你没有调用这些函数,你只是引用它们。例如:

Ponderal
# <function Ponderal at blah>

与:相比:

Ponderal()
# A number