menu

Questions & Answers

While loop will not last in my script [Pyhton]

I have 2 while loops (nested).

However it seems like eventhough I put a 'break' command (at the very end) first while loop (comprehensive one) wont be broken.

Here is the code. What could be the problem?

while(True):
    if(exists(Pattern("1673729480660.png").exact())):
        click(Pattern("1673729480660.png").exact())
        mouseMove((Location(700, 500)))
    X_Coordinate_Region = Region(105,75,30,14)
    Y_Coordinate_Region = Region(139,77,25,11)
    #X_Coordinate = ()
    #Y_Coordinate = Y_Coordinate_Region.text()
    X_temp = X_Coordinate_Region.text()
    Y_temp = Y_Coordinate_Region.text()
    wait(0.15)
    X_Coordinate_Region = Region(105,75,30,14)
    Y_Coordinate_Region = Region(139,77,25,11)
    #X_Coordinate = X_Coordinate_Region.text()
    #Y_Coordinate = Y_Coordinate_Region.text()
    if X_temp==X_Coordinate_Region.text() and Y_temp==Y_Coordinate_Region.text() and arrived==False:
        startMining() //this reiterates the same function
    try:
        if abs(int(X_Coordinate_Region.text())-1490)<30 and abs(int(Y_Coordinate_Region.text())-540)<30:
            type("s")
            mouseMove(mouseMove(Location(0, 500)))
            wait(0.95)
            mouseMove((Location(700, 500)))
            click(Pattern("1673398228807.png").similar(0.50))
            while(!arrived):
                try:
                    if abs(int(X_Coordinate_Region.text())-1453)<30 and abs(int(Y_Coordinate_Region.text())-380)<30:
                        arrived=True
                        type("ss")
                        type(" ")
                except:
                  continue
            break
    except:
        continue
watchMining()

Put a "break" command to end the endless cycle. And

Comments:
2023-01-21 00:30:17
Have you tried using a debugger?
2023-01-21 00:30:17
using sikulix ide, dont have any debugger functionality, or i dont know how :S, actually want to use a better ide to debug, but dont know how to use sikulix functionality on another environment.
Answers(1) :

It appears that the problem is that you have a nested while loop within the outer while loop, and the inner loop does not have a 'break' command. As a result, even though the outer loop's 'break' command is executed, the inner loop continues to run indefinitely. To fix this, you should add a 'break' command within the inner loop's conditional block, so that it will exit when the condition for 'arrived' is met.

Or maybe you just misplaced the break command before the last except it should be like this

            while(!arrived):
                try:
                    if abs(int(X_Coordinate_Region.text())-1453)<30 and abs(int(Y_Coordinate_Region.text())-380)<30:
                        arrived=True
                        type("ss")
                        type(" ")
                except:
                  continue
                break
    except:
        continue
watchMining()

instead of this

            while(!arrived):
                try:
                    if abs(int(X_Coordinate_Region.text())-1453)<30 and abs(int(Y_Coordinate_Region.text())-380)<30:
                        arrived=True
                        type("ss")
                        type(" ")
                except:
                  continue
            break
    except:
        continue
watchMining()