0

I have a bunch of references for different filenames at the start of my code:

_file1_="filename1"
_file2_="filename2"
_file3_="filename3"
_filew1_="anothername1"
_filew2_="anothername2"
_filew3_="anothername3"

etc

I want to make a simple pyautogui loop using:

for x in range(1, 3): #for changing _file_ and _filew_ number each loop

but whenever I try to add str(x) to:

pyautogui.typewrite(_file_)

in any way, it stops seeing 'file' and 'filew' as a references (unresolved reference)

I started coding recently, and I know I can technically just copy / paste my code a bunch changing the number manually each time when file or filew is referenced, but copying it 30+ times is an incredibly dirty solution and would be several thousand lines long

So I'm wondering, what am I doing wrong, and is this the correct way to approach a loop?

@BrokenBenchmark (reprex)

import pyautogui
#filename list
_file1_="filename1"
_file2_="filename2"
_filew1_="filename-w-1"
_filew2_="filename-w-2"

for x in range(1, 3): # I want to be able to increase the number for the refference each loop, but i don't know any way to have it iterate it properly without breaking the references
    pyautogui.typewrite(_file1_) # increase the number each loop somehow using the "range function"
    pyautogui.typewrite(_filew1_) # increase the number each loop somehow using the "range function"

for now i'm just using a workaround:

#loop1
pyautogui.typewrite(_file1_)
pyautogui.typewrite(_filew1_)
#loop2
pyautogui.typewrite(_file2_)
pyautogui.typewrite(_filew2_)

but the problem here is that each loop is around 300 lines long and i have over 30 loops, but it works, was just wondering if i can somehow implement a loop here to make the code nicer

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
batkvit
  • 34
  • 3
  • Could you post a [minimal, *reproducible* example](https://stackoverflow.com/help/minimal-reproducible-example)? – BrokenBenchmark Mar 26 '22 at 02:50
  • 3
    When you want to iterate over multiple values, don't put them in separate variables - make a list or tuple. – Mark Ransom Mar 26 '22 at 03:03
  • keep it as `list` or `dictionary` - ie. `files ={"1": "filename1", "2": "filename2", ...}` – furas Mar 26 '22 at 06:11
  • This is basically yet another duplicate of https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables – tripleee Apr 23 '22 at 16:10

0 Answers0