python中的for与while循环代码题1

问题(分别用for与while做出来):
1、从0-9这10个数字中,选出9个不同数字使得ab+cde+fghi=3102,输出其中未被选中的数字。
试过的方法:
①这个是成功的for方式(我就是while卡住了)

img

②用过其它的想法(但都由于运算量过大运行不出来被pass):
(1)思路:将123456789切片。得到两位数、三位数、四位数、各个一位数
改集合看是否9个,九个则判断两位数+三位数+四位数是否=3102。

img

img

(2)

img

(3)之前试过用9个for循环,但一样的结果啦
谢谢!

对于for 和while , 是可以转换的。
一个是直接遍历, 一个是按长度遍历。
比如你的第一for , 可以这么改

list1 = [n for n in range(1,11)]

print("for")
for i , num in enumerate(list1):
    print(i,num)

print("while")
i = 0
while i < len(list1):
    num = list1[i]
    print(i,num)
    i += 1