2012-05-17 53 views
0

我试图写一个日历。但for循环只会打印出第一个月的日子。如果我尝试打印循环后的日子,它将工作。有谁知道发生了什么事?Python:For循环没有按预期迭代

cal_year = input('Enter a year (YYYY): ') 

def not_common(year): 
    if (year % 4 == 0): 
     if (year % 100 == 0): 
      if (year % 400 == 0): 
       return 1 
      else: 
       return 0 
     else: 
      return 1 
    else: 
     return 0 

leap = not_common(cal_year) 

if leap == 0: 
    print 'This is not a leap year' 

elif leap == 1: 
    print 'This is a leap year' 

else: 
    print 'unexpected calculation error' 

y1900=1 


day = ((cal_year-1900)*365)%7 

month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] 
calendar = [range(1,32), range(1,29), range(1,32), range(1,31), range(1,32), range(1,31),\ 
     range(1,32),range(1,32),range(1,31),range(1,32),range(1,31),range(1,32)] 

if (leap == 1): 
    calendar[1] = range(1,30) 

week = ['su','mo','tu','we','th','fr','sa'] 
r = 0 
for i in range(0,12): 
    print month[i] 
    print ' '.join(str(y) for y in week) 
    for j in range (1, (len(calendar[i])/7)+2): 
     #cal =' '.join(str(x).zfill(2) for x in calendar[i][r*7:7*(r+1)]) 
      print ' '.join(str(x).zfill(2) for x in calendar[i][r*7:7*(r+1)]) 
      r += 1 
    print 

print calendar[1] 
print calendar[11] 

麻烦的线被认为是: '。加入(STR(x)的.zfill(2)在日历X [I] [R * 7:7 *(R + 1)] )

+0

@马克的答案指出了这个程序的一些问题。另一个问题是你计算'日',假定在给定年份和1900年之间没有闰年。这实际上并没有影响代码,因为它不会使用'日'。 –

回答

1

每次开始新月时,您需要重新输入值r

但是,您的代码还有其他问题,您假定每个月都是在星期天开始,并且具有7天的确切倍数。