month = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"]
num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
for i in range(len(month)):
for h in range(len(num)):
print(month[i], "the", num[h])
我怎样才能把数字定为有些月是28号,有些月是31号?目前所有的月份都截止到31号。
以下是我在坚持基本解决问题的方式的同时如何解决问题:
month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
is_leap_year = True
for i in range(len(month)):
for h in range(29 if (month_days[i] == 28 and is_leap_year) else month_days[i]):
print(month[i], "the", h + 1)
请注意,实际上没有必要在列表中列出每天的数字。另外,看看我是如何应对闰年的。它将在2月is_leap_year == True
时增加一天。