-1

Made a script to copy info to an API. How do i repeat this script to run a certain number of times?

Sorry, just started playing around with python for work so i'm not too sure of what i'm doing just yet. Thank you!

import pyautogui as pag
import time
time.sleep (3)

pag.click(448, 98, interval = 0.25) #click NEW bookmark
pag.hotkey('ctrl', 'tab') #switch to spreadsheet; must be on the correct cell
pag.hotkey('ctrl', 'c', interval=0.25) #copies first name from spreadsheet
pag.press('tab') #move to last name cell before switch to API
pag.hotkey('ctrl', 'tab') #switch back to API
pag.scroll(50) #scrolls to view recruiter
pag.click(192, 297) #clicks first name box to have a place to press enter
pag.press('enter') #saves entered information
  • Had to shorten the code down to be able to post :) – Sultan Afsheen Aug 20 '20 at 00:08
  • What you need to learn is ['for loops'](https://www.w3schools.com/python/python_for_loops.asp), namely `for _ in range(n):`. Your question is very general and has nothing to do with `pyautogui` per se. Possible duplicate question: https://stackoverflow.com/questions/10440493/for-loops-novice – Mandera Aug 20 '20 at 06:36

2 Answers2

0

This will run n times.

import pyautogui as pag
import time
time.sleep (3)

for i in range(n):
    pag.click(448, 98, interval = 0.25) #click NEW bookmark
    pag.hotkey('ctrl', 'tab') #switch to spreadsheet; must be on the correct cell
    pag.hotkey('ctrl', 'c', interval=0.25) #copies first name from spreadsheet
    pag.press('tab') #move to last name cell before switch to API
    pag.hotkey('ctrl', 'tab') #switch back to API
    pag.scroll(50) #scrolls to view recruiter
    pag.click(192, 297) #clicks first name box to have a place to press enter
    pag.press('enter') #saves entered information
ndogac
  • 1,185
  • 6
  • 15
0

For just repeating this block of code n number of times without any change to the code in each iteration (repetition), I would suggest using a standard for loop as below:

for i in range(n):
    # Your block of code indented here

If you do not know what this is, I'd suggest you to go through some online resources that teach basics of programming in python.

PS: If you already have an understanding of how Python libraries and APIs work (if not then don't worry, you can learn these yourself in no time if you're motivated), I would suggest checking out certain libraries like selenium that allow you to control a browser through code (without hard coding where to click). Python's main advantage is the plethora of libraries that allow you to do virtually any task on your computer.

Sannat Bhasin
  • 167
  • 1
  • 8