简体   繁体   English

我如何处理 UnboundLocalError

[英]How do I deal with UnboundLocalError

guys.伙计们。 My boss gave me the following Code:我的老板给了我以下代码:

def edge_rollover(data, phi_width, phi_height, phi_width_fine, phi_height_fine):
    length = int(np.size(data) // 2)
    step_width_limit = 0.001    #Grenze der Schrittweite bei nicht äquidistanten Messdaten aus der Helias-Messung 

    einzug = [0, 0]
    k = length - 1
    
    interval_fine = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), 0.01, rtol=0.00005)))

    
    i_0 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), 0.1)))
            
    #Ermittlung der Kanteneinzugsbreite bzw. KE-Start
    for i in range(k, i_0, -1):
        a = data[np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[i,0] - 0.1, 4), rtol=0.00005)), 0].size
        if a != 0:      #nur machen wenn es von data[i,0] auch einen wert gibt der um 0.1 kleiner ist
            x_1 = data[i,0]
            y_1 = data[i,1]
            
            i_1 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[i,0] - 0.1, 4), rtol=0.00005)))
    
            x_2 = data[i_1, 0]
            y_2 = data[i_1, 1]
            
            if (x_2 - x_1) != 0:
                alpha = np.arctan(((y_2 - y_1) / (x_2 - x_1))) * (180 / np.pi)
            else:
                alpha = -90
            if alpha > phi_width and alpha < -phi_width:      #Wird der errechnete Winkel größer als der negative Referenzwinkel, ist von einem Messfehler auszugehen und der Peak wird durch die Winkeleingrenzug übersprungen
                for j in range(i, i_1, -interval_fine):      #Feinabtastung innerhalb der Länge 'interval_start'
                    i_2 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[j,0] - 0.01, 4), rtol=0.00005)))
                    xf_1 = data[j, 0]
                    xf_2 = data[i_2, 0]
                    yf_1 = data[j, 1]
                    yf_2 = data[i_2, 1]
                    if (xf_2 - xf_1) != 0:
                        alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)
                    else:
                        alpha_fine = -90
                    if alpha_fine > phi_width_fine and alpha_fine < -phi_width_fine:
                        einzug = [xf_1, yf_1]
                        break
                break
            if alpha > phi_width and abs(alpha_fine) >= abs(phi_width_fine):      #Für den Fall dass die Feinabtastung kein Ergebnis liefert wird der KE am Ende des Intervalls der Feinabtastung angenommen 
                einzug = [x_2, y_2]     #Der Kanteneinzugsbeginn wird als (x,y)-Koordinaten gespeichert
                
            i_3 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[k, 0]-0.1, 4), rtol=0.00005)))
    
            #Ermittlung der rechtwinkligen Schnittfläche des Schnittteiles (Ende Kanteneinzug) 
            for i2 in range(0, i_3, 1):
                i_4 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[i2, 0]+0.1, 4), rtol=0.00005)))
                x_1 = data[i2, 0]
                x_2 = data[i_4, 0]
                y_1 = data[i2, 1]
                y_2 = data[i_4, 1]
                if (x_2 - x_1) != 0:
                    alpha = np.arctan(((y_2 - y_1) / (x_2 - x_1))) * (180 / np.pi)
                else:
                    alpha = -90
                if alpha < phi_height:
                    for j2 in range(i2, i_4, interval_fine):     #Wird 'phi_height' überschritten (ins negative) wird ab dieser Stelle der Intervallbereich 'interval_end' mit feiner Schrittweite überprüft
                        i_5 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[j2,0]+0.01, 4), rtol=0.00005)))
                        xf_1 = data[j2, 0]
                        xf_2 = data[i_5, 0]
                        yf_1 = data[j2, 1]
                        yf_2 = data[i_5, 1]
                         
                        if (xf_2 - xf_1) < step_width_limit:
                            n = j2 + interval_fine
                            while (xf_2 - xf_1) < step_width_limit and n < (i_4):      #Dieser Block ist für nicht äquidistante Messpunkte nahe Null aus der Helias-Messung
                                n += 1
                                xf_2 = data[n, 0]
                            yf_2 = data[n, 1]
                            if (xf_2 - xf_1) >= step_width_limit:
                                alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)
                            if alpha_fine < phi_height_fine:
                                einzug = np.vstack((einzug, [xf_1, yf_1]))    #Das Ende des Kanteneinzugs wird als (x,y)-Koordinaten gespeichert
                                break
                            j2 = n
                             
                        if (xf_2 - xf_1) >= step_width_limit:
                            alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)                 
                        if alpha_fine < phi_height_fine:
                            einzug = np.vstack((einzug, [xf_1, yf_1]))    #Das Ende des Kanteneinzugs wird als (x,y)-Koordinaten gespeichert
                            break
                    if alpha_fine >= phi_height_fine:             
                        einzug = np.vstack((einzug, [x_1, y_1]))
                    break
      
            if alpha >= phi_height:
                for j3 in range(i, k, interval_fine):     #Wird 'phi_height' nicht überschritten (ins negative) wird die letzte Intervallelänge 'interval_end' des Datensatzes mit feiner Schrittweite überprüft
                    i_6 = int(np.argwhere(np.isclose(np.round_(data[:,0], 4), np.round_(data[j3,0]+0.1, 4), rtol=0.00005)))
                    xf_1 = data[j3, 0]
                    xf_2 = data[i_6, 0]
                    yf_1 = data[j3, 1]
                    yf_2 = data[i_6, 1]
                    
                    if (xf_2 - xf_1) < step_width_limit:    #Dieser Block ist für nicht äquidistante Messpunkte nahe Null aus der Helias-Messung
                        n = j3 + i_0
                        while (xf_2 - xf_1) < step_width_limit and n < k:
                            n += 1
                            xf_2 = data[n, 0]
                        yf_2 = data[n, 1]
                        if (xf_2 - xf_1) >= step_width_limit:
                            alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)
                        if alpha_fine < phi_height_fine:
                            einzug = np.vstack((einzug, [xf_1, yf_1]))    #Das Ende des Kanteneinzugs wird als (x,y)-Koordinaten gespeichert
                            break
                        j3 = n
                    if (xf_2 - xf_1) >= step_width_limit:
                        alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)       
                            
                    #if (xf_2 - xf_1) != 0:
                        #alpha_fine = np.arctan(((yf_2 - yf_1) / (xf_2 - xf_1))) * (180 / np.pi)
                    #else:
                        #alpha_fine = -90
                    if alpha_fine < phi_height_fine:
                        einzug = np.vstack((einzug, [xf_1, yf_1]))    #Das Ende des Kanteneinzugs wird als (x,y)-Koordinaten gespeichert
                        break
                if alpha >= phi_height and alpha_fine >= phi_height_fine:
                    einzug = np.vstack((einzug, [data[k, 0], data[k, 1]]))  #Wird der Winkel von 85 Grad beziehungsweise der Feinwinkel von 78.69 Grad nicht überschritten, so ist davon auszugehen, dass keine Messpunkte auf der Flanke gefunden wurden und der letzte gemessene Punkt das Ende des KE darstellt
            return einzug

If I use it I get the following Error:如果我使用它,我会收到以下错误:

  File "T:\4_Team sws\sws_vk\10_Kanteneinzugskript\test_groß_n´s fertig.py", line 389, in <module>
    einzug = edge_rollover(data, phi_width, phi_height, phi_width_fine, phi_height_fine)

  File "T:\4_Team sws\sws_vk\10_Kanteneinzugskript\test_groß_n´s fertig.py", line 295, in edge_rollover
    if alpha >= phi_height and alpha_fine >= phi_height_fine:

UnboundLocalError: local variable 'alpha_fine' referenced before assignment

Could somebody help me with that problem I know it´s alot of code.有人可以帮我解决这个问题我知道它有很多代码。 I tried to put the last part where alpha_fine is supposedly referenced before being assigned further to the right so it gets referenced after being assigned but I don´t think thats the way to do it.我试图将最后一部分放在应该引用 alpha_fine 的地方,然后再进一步分配到右边,以便在分配后引用它,但我认为这不是这样做的方法。

Assign a default value to alpha_fine.为 alpha_fine 分配一个默认值。 If your conditions are not True, alpha_fine will not be defined.如果您的条件不为真,则不会定义 alpha_fine。 It gives an error at the bottom because it is not defined.它在底部给出了一个错误,因为它没有被定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM