loop click one image only from a list

Asked by The Grey on 2018-07-12

so i'm making this beginner script for a game and i have tried alot of methods but nothing helped me solve my problem.
i want my script to click some specific images as they appear on screen(i can do this no problem) then i will see 15 images which i saved them in a list i want to choose only one image from the list and click it then continue my script and next script run choose the next image from list and so on and repeat this forever.. something which look like this:
while True:
click("1531176838693.png")
wait(5)
click("1531176913033.png")
wait(35)
click("1531177052212.png")
wait(5)
imagelst = [ --i want to choose only one from this list every time i loop here
"1531365554265.png",
"1531365585043.png",
"1531365601372.png",
"1531365618428.png",
"1531365633758.png",
"1531365644149.png",
"1531365656034.png",
"1531365676365.png",
"1531365689264.png",
"1531365698168.png",
"1531365713198.png",
"1531365723352.png",
"1531365738467.png",
"1531365751831.png",
"1531365763188.png"] ------------- then continue my script normally

click("1531182624939.png")
wait(5)
click("1531182731781.png")
wait(3)
click("1531272347054.png")

any help will be appreciated.. thanks

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
The Grey
Solved:
2018-07-13
Last query:
2018-07-13
Last reply:
2018-07-12
Alex (carroll-alex-d) said : #1

Sounds like you want to do a for loop. Python lists are iterate-able. Example:

image_list = ['image1.png', 'image2.png', 'image3.png']

for image in image_list:
    click image

This iterate through the list and click every image, which doesn't seem like what you want to do. It sounds like you want to click a different image every time you loop through. Something like this might work:

image_list = ['image1.png', 'image2.png', 'image3.png']
i = 0
while true:
do_stuff()
click(image_list[i%len(image_list)])
i += 1
do_more_stuff()

Here's the basics of what I did there:

1. Initialize a variable to track what the loop count is.
2. Do your stuff before the loop.
3. Use the modulus operation to determine what index of the list to call. It works like this in my example:
len(image_list) = 3 # or whatever the length of your list i

i = 0
0 % 3 = 0
select the list element at index 0.

i = 1
1 % 3 = 1
select the list element at index 1.

i = 2
2 % 3 = 2
select the list element at index 2.

i = 3 # now we wrap back around to index 1
3 % 3 = 0
select the list element at index 0.

This allows you to run an infinite loop that will sequential select one image from your image list each time.

The Grey (the-grey) said : #2

omg this worked fine for me thank you so much for your help