-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()

when I un the program an error appears. line 84, in td calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age) UnboundLocalError: local variable 'weight' referenced before assignment does anyone know how I can fix this error? the code which is relevant to the question surrounded by '#'

Nilesh
  • 20,521
  • 16
  • 92
  • 148
Kelly
  • 1
  • 1
  • 5

1 Answers1

1

You declare weight here

def perd():

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

...

but you try to use it here in this other function:

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

this is a separate function that does not know about the variables within the perd() function, and why should it? It does not need any of them to operate as functions should isolate pieces of code and be able to be used by themselves. In other words once perd() runs its local variables go out of scope. Take a look at this question, Short Description of the Scoping Rules?. To solve this you either need to make the input taken in perd() global, not advisable because it is not very pythonic. Or you can pass the value in as a function parameter like this:

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

Now you have access to the value passed in as weight within the td function. But you need to realize that these are not the same variables, they may share the same name but they are NOT the same. You will need to pass in all values that you want to use from the perd function. This is preferable because you are designing your functions in a way that they can be used modularity, so one can handle getting input from the user while the other can perform calculations on data that it already knows are valid, this allows for easier to read code, which is what python is all about

Community
  • 1
  • 1
user3282276
  • 3,674
  • 8
  • 32
  • 48