for循环问题
for year in range (1900,2100):for month in range (1,13):
for day in range (1,32):
if month < 10 & day < 10:
key = str(year) + '0' + str(month) + '0' + str(day)
elif month < 10 & day >= 10:
key = str(year) + '0' + str(month) + str(day)
elif month >= 10 & day < 10:
key = str(year) + str(month) + '0' + str(day)
else:
key = str(year) + str(month) + str(day)
print(key)
代码如上,目的是打印1900至2100的所有年月日,若月份和日期为各位数则补0,不知道为啥出来的结果不正确,求助大佬!-_- 问题解决了,把&换成and就行了.... 本帖最后由 suchocolate 于 2021-3-27 17:01 编辑
直接format,文本格式化知识:https://www.runoob.com/python/att-string-format.html
for year in range(1900, 2100):
for month in range(1, 13):
for day in range(1, 32):
print('{}{:0>2d}{:0>2d}'.format(year, month, day)) 看看我给你修改的for year in range (1900,2100):
for month in range (1,13):
if month == 2 and (year%4 == 0 and year%100 != 0 or year%400 == 0):
days = 29
elif month == 2:
days = 28
elif month in :
days = 31
else:
days = 30
for day in range(1,days + 1):
date =
key = "-".join(date)
print(key)
页:
[1]