growthndevlpmnt
growthndevlpmnt

Reputation: 3

Python: For loop not iterating as expected

I have tried to write a calendar. But the for loop will only print out the days for the first month. if i try to print the days after the loop it will work. Does anyone know what is going on?

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]

The trouble line is believed to be: ' '.join(str(x).zfill(2) for x in calendar[i][r*7:7*(r+1)])

Upvotes: 0

Views: 675

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308206

You need to start over with your value r each time you start a new month.

Your code has other problems though, you're assuming each month starts on a Sunday and has an exact multiple of 7 days.

Upvotes: 1

Related Questions