你的代碼有很多問題;首先格式化它更好地確保您對您的代碼有意見(以#開始的行)
也不要直接將字符串的單位轉換爲浮點數。如果他們輸入無效的句柄異常會怎麼樣。
第三,你格式化輸出文本的方式是非常糟糕的,它很難閱讀所有的逗號和副詞。
此外,您獲得的值的方式,你從來沒有將它們設置爲變量你也使用這個健康功能,你不需要直接調用值!
還可以使用無意義的名字變量而不是NAME2使用名字等
您的代碼應更好如下所示(請注意,如果這是家庭作業,你把這個在你的教授將容易找到它計算器;所以不要)
# 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()
你使用的是什麼版本的Python?你也需要在函數中縮進你的代碼。 – Zimm3r
另外它沒有真正的原因,你需要兩個打印三明治你的主要功能調用 – Zimm3r
Im使用Python 3.3.2 – Moose