Python nested while loop is not working correctly

I'm testing a nested while loop and running into some issues and wondering if anyone can find what I'm doing wrong.

The while loop is supposed to continue to print x,y pairs as it works towards a final x value. The output, however, is not working for the nested loop. In that, it's not updating the y value after it reaches a certain value. Code and example output are shown below:

IWX1 = 2317715.221
IWY1 = 796309.685
IWX2 = 2317743.658
IWY2 = 796346.704
IWX3 = 2317690.718
IWY3 = 796343.693
Q = 5

MWX1 = 2317704.666
MWY1 = 796321.216
Z1 = ((Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY1)/(MWX1-IWX1))) + (Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY2)/(MWX1-IWX2))) 
        + (Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY3)/(MWX1-IWX3))))

X_t = IWX1 - 0.01
Y_t = IWY1
Z_t = 7

while abs((X_t - MWX1))>0.01:
    while abs(Z_t-Z1)>0.05:
        Z_t = ((Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY1)/(X_t-IWX1))) + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY2)/(X_t-IWX2))) 
        + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY3)/(X_t-IWX3))))
        Y_t += 0.01
    else:
        print(X_t,Y_t)
        X_t -= 0.01
Y_t += 0.01

Example output:

2317715.211 796309.7250000001
2317715.2010000004 796309.7350000001
2317715.1910000006 796309.7450000001
2317715.181000001 796309.7550000001
2317715.171000001 796309.7650000001
2317715.1610000012 796309.7750000001
2317715.1510000015 796309.7850000001
2317715.1410000017 796309.7950000002
2317715.131000002 796309.8050000002
2317715.121000002 796309.8150000002
2317715.1110000024 796309.8250000002
2317715.1010000026 796309.8350000002
2317715.091000003 796309.8450000002
2317715.081000003 796309.8550000002
2317715.0710000033 796309.8650000002
2317715.0610000035 796309.8750000002
2317715.0510000037 796309.8850000002
2317715.041000004 796309.8950000003
2317715.031000004 796309.9050000003
2317715.0210000044 796309.9150000003
2317715.0110000046 796309.9250000003
2317715.001000005 796309.9350000003

.
.
.
.
2317704.6810002355 796320.2550000099

Notice the Y's are changing but when calculating the Z value it does not meet the criteria required to exit the inside while loop.

EDIT - I added Y-t += 0.01 outside of the inside loop and i thought that fixed the problem but it didn't, it's no longer trying to satisfy the inside loop - can someone show me how to fix it so continues to run the inside loop until the outside loop is satisfied?

FINAL EDIT - if i added the Z_t calc outside of the first while loop then it worked thanks to the answer Khaled posted. See below:

IWX1 = 2317715.221
IWY1 = 796309.685
IWX2 = 2317743.658
IWY2 = 796346.704
IWX3 = 2317690.718
IWY3 = 796343.693
Q = 5

MWX1 = 2317704.666
MWY1 = 796321.216
Z1 = ((Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY1)/(MWX1-IWX1))) + (Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY2)/(MWX1-IWX2))) 
        + (Q/ (2 * np.pi)) * (np.arctan((MWY1-IWY3)/(MWX1-IWX3))))

X_t = IWX1 - 0.01
Y_t = IWY1
Z_t = 7

while abs((X_t - MWX1))>0.01:
    while abs(Z_t-Z1)>0.05:
        Z_t = ((Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY1)/(X_t-IWX1))) + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY2)/(X_t-IWX2))) 
        + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY3)/(X_t-IWX3))))
        Y_t += 0.01
    else:
        print(X_t,Y_t)
        X_t -= 0.01
        Z_t = ((Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY1)/(X_t-IWX1))) + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY2)/(X_t-IWX2))) 
        + (Q/ (2 * np.pi)) * (np.arctan((Y_t-IWY3)/(X_t-IWX3))))
        Y_t += 0.01


1 answer

  • answered 2022-01-19 16:53 Khaled Bagh

    If you look at your code, once the inner while loop condition is satisficed once, you're not changing/resetting the values of either Z_t or Z1. Therefor, in the following loops, the condition is already satisfied and the y value will not change, only x will change.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum