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

代码问题执行顺序

我的代码:

L=[]

for a in range(1,10):

    for b in range(0,10):

        for a in range(1,10):

            L.append(a*100+b*10+a)

print L

结果为:

[101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999, 101, 202, 303, 404, 505, 606, 707, 808, 909, 111, 212, 313, 414, 515, 616, 717, 818, 919, 121, 222, 323, 424, 525, 626, 727, 828, 929, 131, 232, 333, 434, 535, 636, 737, 838, 939, 141, 242, 343, 444, 545, 646, 747, 848, 949, 151, 252, 353, 454, 555, 656, 757, 858, 959, 161, 262, 363, 464, 565, 666, 767, 868, 969, 171, 272, 373, 474, 575, 676, 777, 878, 979, 181, 282, 383, 484, 585, 686, 787, 888, 989, 191, 292, 393, 494, 595, 696, 797, 898, 999]

有两个问题:

为何我的结果有很多遍呢?

关于for循环嵌套语句的执行:(我理解的执行循环)

第一个for循环里面,还有两个嵌套for循环,那么当第一次执行时,a=1,b=0,a=1,结果为101;第二次,应该是跳回第二个for语句这里,a=1,b=1,a=1.结果为111;第三次为a=1,b=2,a=1,结果为121;这样才对。

为何结果是101、202、303这样的循环呢?



正在回答

1 回答

第一次的101打印之后,循环在最内层的for里面,此时给a赋值为2,就变成了你看到的202。注意,最内层的a,还没执行完,不会跳到第二个for

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
初识Python
  • 参与学习       758625    人
  • 解答问题       8667    个

学python入门视频教程,让你快速入门并能编写简单的Python程序

进入课程

代码问题执行顺序

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信