我用这组代码得到一个UnboundLocalError

时间:2016-03-06 06:54:01

标签: python

import math

'''Parameters to input: NS, EW, degee, minute, second
What the function does: converts bearing to azimuth
Expected return value: azimuth'''
def toAz(v,w,x,y,z):
    if v == "W" and w == "S" and x >= 0 and x < 90:
        azimuth = x,"deg",y,"min",z,"sec"
    if v == "W" and w == "N" and x >= 90 and x < 180:
        azimuth = (179-x),"deg",(60-y),"min",(60-z),"sec"
    if v == "E" and w == "N" and x >= 180 and x < 270:
        azimuth = (180+x),"deg",(y),"min",(z),"sec"
    if v == "E" and w == "S" and x >= 270 and x < 360:
        azimuth = (270+(89-x)),"deg",(60-y),"min",(60-z),"sec"
    return azimuth

1 个答案:

答案 0 :(得分:0)

您需要在函数定义的顶部定义azimuth变量。因为return azimuth期望azimuth变量出现在同一级别。

def toAz(v,w,x,y,z):
    azimuth = None
    if v == "W" and w == "S" and x >= 0 and x < 90:
        azimuth = x,"deg",y,"min",z,"sec"
    if v == "W" and w == "N" and x >= 90 and x < 180:
        azimuth = (179-x),"deg",(60-y),"min",(60-z),"sec"
    if v == "E" and w == "N" and x >= 180 and x < 270:
        azimuth = (180+x),"deg",(y),"min",(z),"sec"
    if v == "E" and w == "S" and x >= 270 and x < 360:
        azimuth = (270+(89-x)),"deg",(60-y),"min",(60-z),"sec"
    return azimuth