你的代码有很多问题;首先格式化它最好确保你对你的代码有注释(以#开头的行)
也不要直接将单位从字符串转换为浮点数。如果他们输入一些无效的处理异常怎么办。
第三,你输出文本的格式太糟糕了,阅读所有的逗号和括号非常困难。
此外,您获取从未将它们设置为变量的值的方式也可以使用此健康功能,您不需要直接调用这些值!
还对变量使用有意义的名称而不是 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()