为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUWWW 2022-07-05 18:48:35
我正在尝试更改 for 循环的索引,具体取决于用户是否要转到上一个图像(索引 = 索引 - 1)以及是否要转到下一个图像(索引 = 索引 + 1)但是,索引没有改变外循环(在 if 语句之外)。flag = Falsewhile 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       提前致谢。
查看完整描述

1 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

不知道为什么存在 for 循环,我很想将它的结构更像这样:


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     


查看完整回答
反对 回复 2022-07-05
  • 1 回答
  • 0 关注
  • 209 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号