在 Python 中更改 For 循环的索引(在 if 语句之外)

我正在尝试更改 for 循环的索引,具体取决于用户是否要转到上一个图像(索引 = 索引 - 1)以及是否要转到下一个图像(索引 = 索引 + 1)但是,索引没有改变外循环(在 if 语句之外)。


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       

提前致谢。


HUWWW
浏览 210回答 1
1回答

慕斯709654

不知道为什么存在 for 循环,我很想将它的结构更像这样:flag = Falseidx = 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     
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python