2013-10-12 15 views
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,",",")") 

***應該沿着 最後,第一(114:35.234,出生體重指數:16.5,羅勒的指數BMI)行返回的東西其他人的幫助。這個練習的全部目的是創建功能並回復給他們。爲什麼我的代碼不工作?在介紹 林到Python類和它真的晚與任何人聯繫

編輯:非常感謝您的幫助球員,我知道了很多關於這裏的問題是通常先進得多,但快速答覆和有用的提示量是極大的讚賞。

+0

你使用的是什麼版本的Python?你也需要在函數中縮進你的代碼。 – Zimm3r

+0

另外它沒有真正的原因,你需要兩個打印三明治你的主要功能調用 – Zimm3r

+0

Im使用Python 3.3.2 – Moose

回答

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. 不要命名變量,如函數!

你的代碼會做得更好。

+0

你是絕對的生命保護者。這有很大的幫助,因爲我可以看到我剛擰上代碼的部分。 – Moose

+0

如何更改逗號的位置?這對我來說是最後幾項任務的一個問題。幸運的是,大部分時間都是一個字符串。身份證需要 體重指數:17.301038062283737,Ponderal索引 變成 BMI:17.301038062283737,Ponderal索引 – Moose

+0

你是什麼意思,改變逗號的位置? – aIKid

0

你不是在調用這些函數,而只是引用它們。例如:

Ponderal 
# <function Ponderal at blah> 

相比:

Ponderal() 
# A number 
+0

代碼假設只提出4個問題,然後計算3個計算,然後最終打印它們。實際代碼中的唯一部分(不是函數)必須是「main()」 – Moose

+0

@BryceDressler我編輯了我的答案 – TerryA

1

你的代碼有很多問題;首先格式化它更好地確保您對您的代碼有意見(以#開始的行)

也不要直接將字符串的單位轉換爲浮點數。如果他們輸入無效的句柄異常會怎麼樣。

第三,你格式化輸出文本的方式是非常糟糕的,它很難閱讀所有的逗號和副詞。

此外,您獲得的值的方式,你從來沒有將它們設置爲變量你也使用這個健康功能,你不需要直接調用值!

還可以使用無意義的名字變量而不是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() 
+0

您會建議如何清理打印字符串? – Moose

+0

'return Health'(健康評估爲封閉函數) - 我認爲不是。 – user2864740

+0

@BryceDressler查看我使用的打印字符串。 – Zimm3r

相關問題