简体   繁体   中英

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) 

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.

Great question,

Firstly make sure you have all dependencies installed for your OS. find out more here

pip3 install pyautogui

open terminal and run :

python3

import pyautogui
for i in range(0, 10):
    pyautogui.typewrite("data%d, " % i)

Hope this helps. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM