Search code examples
pythonpython-3.xpyautogui

How do I cycle in "pyautogui" and write each number in the cycle?


I'd like to write a cycle that'd write:

data1, data2, data3, data(n)

Here is my python code:

import pyautogui
for i in 2:
pyautogui.typewrite("data"$i"")

Thank you.

import pyautogui
for i in range(1, 5):
    pyautogui.click(476, 679) 
    pyautogui.click(clicks=3) 
    pyautogui.typewrite(['delete']) 
    value = -0.5 + (i - 1) * 0.00001 
    pyautogui.typewrite("value")  
    pyautogui.typewrite(['enter'])
    pyautogui.click(169, 681) 
    pyautogui.click(330, 685) 
    pyautogui.click(448, 174) 
    pyautogui.typewrite("data{}".format(i))
    pyautogui.click(978, 664) 

Solution

  • The problem is that you have several syntax errors in your code and you're not using the for loop correctly.

    In order of occurrence:

    1. the i variable is a value not an index, to get an index you need to use the enumerate function
    2. the number 2 isn't a valid variable name, try changing it to: two
    3. there is no $ formatting in python, use the format method instead

    Here, try replacing your code with this:

    import pyautogui
    
    for i in range(1, 5):
      pyautogui.typewrite("data{}".format(i))
    

    NOTE: I don't mean to sound rude but I'd suggest checking out Sentdex's Python3 basics series, which is what I used to get started with Python.

    UPDATE: to get a range of numbers (without having to type each number manually) use the range function instead.

    Good luck.