Python 未绑定的本地错误,我不知道如何修复

Python 未绑定的本地错误,我不知道如何修复,python,python-3.x,Python,Python 3.x,当我取消程序时,会出现一个错误。 第84行,td格式 calburn1=(13.75*体重)+(5*身高)-(6.67*年龄) UnboundLocalError:分配前引用的局部变量“权重” 有人知道我如何修复这个错误吗? 与由“#”包围的问题相关的代码您在此处声明重量 def perd(): Personaldetails_file = open("Personaldetails_file.txt", "w") Personaldetails_file = open("Pe

当我取消程序时,会出现一个错误。 第84行,td格式 calburn1=(13.75*体重)+(5*身高)-(6.67*年龄) UnboundLocalError:分配前引用的局部变量“权重” 有人知道我如何修复这个错误吗?
与由“#”包围的问题相关的代码

您在此处声明重量

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()
但您尝试在其他函数中使用它:

def perd():

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

...
这是一个单独的函数,它不知道
perd()函数中的变量,为什么要这样做?它不需要它们中的任何一个来操作,因为函数应该隔离代码片段并能够自己使用。换句话说,一旦
perd()
运行,其局部变量就超出范围。看看这个问题。要解决这个问题,您需要将
perd()
中的输入设置为全局,这是不可取的,因为它不是很像python。或者,可以将值作为函数参数传入,如下所示:

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

现在,您可以在td函数中访问作为权重传入的值。但是你需要意识到这些变量是不同的,它们可能有相同的名字,但是它们不一样。您需要从perd函数中传入所有要使用的值。这是更可取的,因为您正在以模块化的方式设计函数,这样一个函数可以处理从用户获取的输入,而另一个函数可以对它已经知道是有效的数据执行计算,这使得代码更易于阅读,这就是python的全部内容,在python中缩进是语法的一部分。请正确缩进代码。如何调用函数td或perd?如果希望
权重
变量与最初设置为
int(输入(“以千克为单位的权重”)
的变量相同,则需要将
全局权重
作为函数体的第一行。。。否则python不知道您需要一个全局变量。@Kelly我更新了格式,请试试这个。@Alec我如何使变量成为全局变量?
td(weight, height, age):
    #code here