0

I am a beginner in python, and I am learning web automation. Let's say I am using pyautogui as pg

import pyautogui as pg
pg.moveTo(100, 150, 1)  # loop should start again from this line 2
pg.click(170, 670, 1)
pg.moveTo(460, 790, 1)
pg.doubleClick(172, 140, 1)
pg.moveTo(450, 100, 1)
pg.click(940, 350, 1)'''

Now I want to repeat this block of code 10 times, how can I achieve that? Without using my interaction with computer? I don't want to copy and paste this code 10 more times. I want this to stop automatically, when it's complete looping 10 times, without taking too much extra lines.

2 Answers2

2

While @moin205's answer worked, it's not Pythonic. A more Pythonic answer would be to put it in a for loop, like so:

for i in range(10):
    new_function()
Seth
  • 2,214
  • 1
  • 7
  • 21
0

You can try moving your code block to a function, and run that function through a while loop, e.g.

def new_function():
   pg.moveTo(100, 150, 1)  # loop should start again from this line 2
   pg.click(170, 670, 1)
   pg.moveTo(460, 790, 1)
   pg.doubleClick(172, 140, 1)
   pg.moveTo(450, 100, 1)
   pg.click(940, 350, 1)

i = 0 
while i < 10: 
   new_function() 
   i += 1
sushanth
  • 8,275
  • 3
  • 17
  • 28
moin205
  • 19
  • 3