Python while while循环另一个while循环只运行一次

时间:2015-10-18 11:29:22

标签: python

我想从2001年到2015年产生一年+几个月。但是,我的while循环仅运行2001年并停止

date =[]

a= 200000
b = 0

while a < 201500:
    a+=100



    while b < 12:
        b+=1



       date.append(a+b)
print  date

请您告诉我如何修复我的代码,结果如下所示?

date =['200101', '200102', '200103','200104','200105','200106','200107','200108','200109','200110','200111','200112', '200201', '200202', '200203', '200204', '200205', '200206', '200207', '200208', '200209', '200210', '200211', '200212', '200301', '200302', '200303', '200304', '200305', '200306', '200307', '200308', '200309', '200310', '200311', '200312', '200401', '200402', '200403', '200404', '200405', '200406', '200407', '200408', '200409', '200410', '200411', '200412', '200501', '200502', '200503', '200504', '200505', '200506', '200507', '200508', '200509', '200510', '200511', '200512', '200601', '200602', '200603', '200604', '200605', '200606', '200607', '200608', '200609', '200610', '200611', '200612', '200701', '200702', '200703', '200704', '200705', '200706', '200707', '200708', '200709', '200710', '200711', '200712', '200801', '200802', '200803', '200804', '200805', '200806', '200807', '200808', '200809', '200810', '200811', '200812', '200901', '200902', '200903', '200904', '200905', '200906', '200907', '200908', '200909', '200910', '200911', '200912', '201001', '201002', '201003', '201004', '201005', '201006', '201007', '201008', '201009', '201010', '201011', '201012', '201101', '201102', '201103', '201104', '201105', '201106', '201107', '201108', '201109', '201110', '201111', '201112', '201201', '201202', '201203', '201204', '201205', '201206', '201207', '201208', '201209', '201210', '201211', '201212', '201301', '201302', '201303', '201304', '201305', '201306', '201307', '201308', '201309', '201310', '201311', '201312', '201401', '201402', '201403', '201404', '201405', '201406', '201407', '201408', '201409', '201410', '201411', '201412', '201501', '201502', '201503', '201504', '201505', '201506', '201507', '201508', '201509', '201510', '201511', '201512']

1 个答案:

答案 0 :(得分:1)

只将b设置为0.下一次,永远不会输入内部循环:

date =[]
a= 200000
while a < 201500:
    a+=100
    b = 0
    while b < 12:
        b+=1
        date.append(a+b)
print date

但最好使用for - 循环:

date = [a * 100 + b for a in range(2001, 2016) for b in range(1, 13)]
相关问题