2014-11-25 59 views
-2
def perd(): 

    Personaldetails_file = open("Personaldetails_file.txt", "w") 
    Personaldetails_file = open("Personaldetails_file.txt", "a") 

    pd = input("Are you a new user?") 

    if pd == "yes": 
     print ("Add your details") 
    ####################################  
    age = int(input("how old are you?")) 
    DOB = input("Date of birth:") 
    gender = input("Gender:") 
    weight = int(input("weight in kg:")) 
    height = int(input("height in cm:")) 

    Personaldetails = (age, DOB, gender, weight, height) 

    Personaldetails_file.write(str(Personaldetails)) 
    Personaldetails_file.close() 

    ####################################### 


def td(): 

    choice = input("which trainning event would you like to access?\n1.swimming\n2.cycling\n3.running\nplease type in the number before the event of which you want to choose\n") 
    if choice == "1": 
     Swimming_file= open("Swimming_file.txt", "w") 

     totaldistance = input("what was the total distance you swam in meters?") 
     totaltime = input("how long did you swim for in minutes?") 
     speed = totaldistance/totaltime 
     print ("on average you where running at a speed of", speed, "mps\nyou can look at the tables to see how many calouries you have burnt") 

     total = (totaldistance, totaltime, speed) 
     Swimming_file.write(str(total)) 
     Swimming_file.close() 

    elif choice == "3": 
     Running_file= open("Running_file.txt", "w") 


     totaldistanceR = int(input("what was the total distance you ran in KM?")) 
     totaltimeR = int(input("how long did you run for in minutes?")) 
     totaltimeR1 = 60/totaltimeR 
     speedR1 = totaldistanceR/totaltimeR1 
     calburn = (speedR1 * 95)/(60/totaltimeR1) 

     print ("\nThe records have been saved") 
     print ("\non average you where running at a speed of", speedR1, "KMph\nyou burnt",calburn," calouries\n") 

     totalR = (totaldistanceR, totaltimeR, speedR1, calburn) 
     Running_file.write(str(totalR)) 
     Running_file.close() 
    ############################################################## 
    elif choice == "2": 
     Cycling_file= open("Cycling_file.txt", "w") 
     with open("Personaldetails_file.txt", "r") as f: 
     content = [x.strip('\n') for x in f.readlines()] 
     lines = f.readlines() 
     for line in lines: 
      words = line.split(",") 
      age = (line.split)(0) 
      weight = (line.split)(3) 
      height = (line.split)(4) 
################################################################    
     totaldistancec = int(input("what was the total distance you cycled in KM?")) 
     totaltimec = int(input("how long did you cycle for in minutes?")) 
     calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age)     
     speedc = totaldistancec/totaltimec 
     print ("on average you where running at a speed of", speedc, "KMph\nyou burnt", calburn1, " calouries") 

     totalc = (totaldistancec, totaltimec, speedc) 
     Cycling_file.write(str(totalc)) 
     Cycling_file.close() 
     Personaldetails_file.close() 

當我解開程序時出現錯誤。 線84,在TD calburn1 =(13.75 *重)+(5×高度) - (6.67 *年齡) UnboundLocalError:分配 之前引用局部變量 '重量' 沒有人知道我該如何解決這個問題? 這是有關通過「#」包圍了問題的代碼未綁定本地錯誤,我不知道如何修復

+1

在Python中,縮進是語法的一部分。請正確縮進您的代碼。 – 2014-11-25 07:12:05

+0

你如何調用你的函數td或perd? – 2014-11-25 07:13:20

+0

如果你想'weight'變量與你初始設置爲'int(input(「weight in kg:」))''相同,你需要把'global weight'作爲函數體的第一行。否則python不知道你想要一個全局變量。 – Alec 2014-11-25 07:14:38

回答

1

您申報重量這裏

def perd(): 

... 
    gender = input("Gender:") 
    weight = int(input("weight in kg:")) 
    height = int(input("height in cm:")) 

... 

,但你嘗試在這個其他功能在這裏使用它:

def tr(): 
    .... 
    calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age) 
    .... 

這是一個單獨的函數不知道perd()函數中的變量,爲什麼要這樣呢?它不需要它們中的任何一個來作爲功能進行操作,以隔離代碼段並且能夠被自己使用。換句話說,一旦perd()運行,其局部變量超出範圍。看看這個問題,Short Description of the Scoping Rules?。要解決這個問題,你需要在perd()全球範圍內輸入,不可取,因爲它不是pythonic。或者,您可以將值作爲函數參數如下所示:

td(weight, height, age): 
    #code here 

現在您可以訪問td函數中作爲權重傳入的值。但是你需要認識到這些變量不是同一個變量,它們可能共享相同的名稱,但它們不一樣。您將需要從perd函數中傳入想要使用的所有值。這是可取的,因爲您正在設計功能的方式可以使用模塊化,所以可以處理來自用戶的輸入,而另一個可以對已知的有效數據執行計算,這使得更容易閱讀代碼,這是python的全部內容

相關問題