Home > Mobile >  How to use another function's return value as an argument for another function? Name Error foun
How to use another function's return value as an argument for another function? Name Error foun

Time:04-22

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!

CodePudding user response:

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

CodePudding user response:

def convert(bear1):
    # ...

def latdep(dist1, azi):
    # remove the 'azi' line
    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




dist1 = # define dist1
bear1 = # define bear1
latdep(dist1, convert(bear1))

CodePudding user response:

just call latdep() without azi. but you forgott to pass bear1 into the function so just change them with each other.

example:

a, b = latdep(dist1, bear1)

and change the second argument:

def latdep(dist1, bear1):
  • Related