Python how to repeat my code

Asked

Viewed 165 times

0

I would like to repeat this code not to iterate it 300 times , is a robot to click on the screen,I wanted it to repeat , the code is this ,I am new here and in programming I did not put the format of the question here and it was not like the code in vscode but it is basically this hehe

import pyautogui


import time




pyautogui.moveTo(28,295,duration=0.5)

pyautogui.click(30,295,duration=0.5)



pyautogui.moveTo(650,220,duration=1)

pyautogui.click(650,220,duration=1)


pyautogui.moveTo(650,670,duration=0.3)

pyautogui.click(650,670,duration=1)

2 answers

3


For this you can use the while loop or the for loop:

import pyautogui
import time

i = 0

while i < 300:

    pyautogui.moveTo(28,295,duration=0.5)
    pyautogui.click(30,295,duration=0.5)

    pyautogui.moveTo(650,220,duration=1)
    pyautogui.click(650,220,duration=1)

    pyautogui.moveTo(650,670,duration=0.3)
    pyautogui.click(650,670,duration=1)

    i += 1
import pyautogui
import time

for _ in range(300):

    pyautogui.moveTo(28,295,duration=0.5)
    pyautogui.click(30,295,duration=0.5)

    pyautogui.moveTo(650,220,duration=1)
    pyautogui.click(650,220,duration=1)

    pyautogui.moveTo(650,670,duration=0.3)
    pyautogui.click(650,670,duration=1)
  • thank you very much guy thank you

  • cara ta dar um erro no console invalid syntax (<unknow>,line8) pylint(syntax-error) [8,10]

  • the syntax of the answer is correct. I suggest you compare how you typed the code there, with what is here. Remembering that you have to configure the tool you are using to edit code to use "spaces" and not "tabs" (or 'tabs' , you will know how the translation is)

  • I am using the vscode , it does not let do the while that shit , I do not know change to spaces hahahah I will search here worth

-6

import pyautogui
import time

i = 0

while i < 300:

    pyautogui.moveTo(28,295,duration=0.5)
    pyautogui.click(30,295,duration=0.5)

    pyautogui.moveTo(650,220,duration=1)
    pyautogui.click(650,220,duration=1)

    pyautogui.moveTo(650,670,duration=0.3)
    pyautogui.click(650,670,duration=1)

    i += 1
  • 2

    Besides the code has no explanation what this answer has different from the answer already accepted?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.