0

I am trying to change the index of a for loop depending if a user wants to go to the previous image (index = index - 1) and if he wants to go the next image (index = index + 1) however, the index doesnt change in the outerloop (outside the if statements).

flag = False
while flag == False:
    for i in range(len(all_image)):

        image = all_image[i]
        print(i)
        userchoice = easygui.buttonbox(msg, image = image, choices=choices)

        if userchoice == 'Next':
            i = i+1

        elif userchoice == 'Previous':
            i = i-1

        elif userchoice == 'cancel':
            print('test')
            flag = True
            break       

Thanks in advance.

2
  • you can't do this with for-loop. It will always get next value from range(). Commented Jan 21, 2020 at 13:05
  • 1
    This is it: you simply can't do that with a for loop - you need a while loop Commented Jan 21, 2020 at 13:06

1 Answer 1

0

Not sure why the for loop is there, I'd be tempted to structure it more like so:

flag = False
idx = 0 #idx is where we are in the image list, and it gets modified by use choices in the while loop.
while flag == False:
    image = all_image[idx]
    print(idx)
    userchoice = easygui.buttonbox(msg, image = image, choices=choices)

    if userchoice == 'Next':
        idx += 1
    elif userchoice == 'Previous':
        idx -= 1
    elif userchoice == 'cancel':
        print('test')
        flag = True
        break       
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! The solution works. Yea, looking at it now, im not sure why I included a For loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.