Search code examples
pythonfunctionnameerror

How to use another function's return value as an argument for another function? Name Error found


i'm a beginner in python and I've been stuck in this problem. I'm trying to use the return value of one function as an argument to the other but I would get a name error:

NameError: name 'azi' is not defined

this is my code

 def convert(bear1):
     azi = []
     dms = []
     dd1 = []
     for x in bear1:
         a = x.split(" ")
         b = a[1].split("-")
         dms = [float(m) for m in b]
         d, m, s = dms
         dd = d + float(m)/60 + float(s)/3600

         if "N" in a and "W" in a:
             az = 180 - dd
             azi.append(az)
        
         elif "S" in a and "W" in a:
             az = dd
             azi.append(az)
        
         elif "N" in a and "E" in a:
             az = 180 + dd
             azi.append(az)
       
         elif "S" in a and "E" in a:
             az = 360 - dd
             azi.append(az)
     return azi

def latdep(dist1, azi):
     azi = convert(bear1)
     lat = []
     dep = []
     for dst, ang in zip(dist1, azi):
         lat.append(-dst * math.cos(math.radians(ang)))
         dep.append(-dst * math.sin(math.radians(ang)))
     return lat, dep

thank you!


Solution

  • When you define a function, you have to define which variables are going to be used by the function, def(var1, var2, ...). In your latdep function, it seems you want azi to be what the convert function returns, however you are trying to define this within the function rather than in the var2 position. (As commented) your first line in latdep overrides your azi input.

    IIUC, You want to remove the line azi = convert(bear1) from latdep, and call the function as below (assuming dist1 and bear1 are already defined):

    latdep(dist1, convert(bear1))