Search code examples
pythonloopsanacondapyautogui

How to write a loop until (Python Anaconda)


I am using the Jupyter Notebook package and I would like to iterate the following commands a fixed number of times. Precisely, the script writes a range of width k=4347 and for each iteration this range should roll, until we get N= 798746.

One iteration is given by the following commands:

pyautogui.click(785, 263)
time.sleep(5)
pyautogui.click(885,11)
pyautogui.click(181, 347)
pyautogui.typewrite('**360795**', 0.25)
time.sleep(1)
pyautogui.click(292, 432)
pyautogui.typewrite('**365141**', 0.25)
time.sleep(1)
pyautogui.click(1348, 699)
time.sleep(180)
pyautogui.click(1335, 212)

I just want to iterate it in order not to do the "copy and paste" and then writing the ranges manually. Any suggestions?


Solution

  • Your question is very confusing to me, but I think what you want it is something like:

    Given a "work" you want to repeat multiple times:

    def work():
        pyautogui.click(785, 263)
        time.sleep(5)
        pyautogui.click(885,11)
        pyautogui.click(181, 347)
        pyautogui.typewrite('**360795**', 0.25)
        time.sleep(1)
        pyautogui.click(292, 432)
        pyautogui.typewrite('**365141**', 0.25)
        time.sleep(1)
        pyautogui.click(1348, 699)
        time.sleep(180)
        pyautogui.click(1335, 212)
    

    You can loop over it while k is less than N by doing:

    k = 0
    N = 798746
    while k < N:
        work()
        k += 4347
    

    That is just a didactic example, there're more elegant ways of doing that.

    UPDATE:

    Okay, I finally understood:

        N = 798746
        bold_number_value_1 = 360795
        bold_number_value_2 = 365141
        while bold_number_value_1 < N and bold_number_value_2 < N:
            pyautogui.click(785, 263)
            time.sleep(5)
            pyautogui.click(885,11)
            pyautogui.click(181, 347)
            pyautogui.typewrite('**{}**'.format(bold_number_value_1), 0.25)
            time.sleep(1)
            pyautogui.click(292, 432)
            pyautogui.typewrite('**{}**'.format(bold_number_value_2), 0.25)
            time.sleep(1)
            pyautogui.click(1348, 699)
            time.sleep(180)
            pyautogui.click(1335, 212)
            bold_number_value_1 += 4347
            bold_number_value_2 += 4347