please copy and save this code.py and enter (without quotes) a day such as "Mer" and then a non leap year like 2019 and notice leap day is still showing up in the print out. i need to delete tuple [3] if it is a leap year and then let it print the 'tup' list after tuple[3] has been excluded from the list
EDIT CHANGING ALL EDITS MENTIONED BY CONFIRMED ANSWERER RESULTED IN GOOD CODE AND I FOUND THAT THE BUG WAS I MISVALUED LEAP DAY RANGE AS 70 WHEN IT SHOULD BE 69 IN ORDER TO GET THE CODE WORKING. THANK YOU.
import numpy as np
def make_calendar(year, start_day):
tup = [('Early Winter', range(1, 37, + 1)),
('Mid Winter 37,', range(37, 1, + 1)),
('Late Winter ', range(38, 71, + 1)),
('Leap Day ', range(70, 69 + 1)),#tup[3] is this line i need to delete if not leap year
('Late Winter ', range(71, 74, + 1)),
('Early Spring ', range(1, 37, + 1)),
('Mid Spring 37,', range(37, 1, +1)),
('Late Spring ', range(38, 74, + 1)),
('Early Summer ', range(1, 37, + 1)),
('Mid Summer 37,', range(37, 1, +1)),
('Late Summer ', range(38, 74, + 1)),
('Early Autumn ', range(1, 37, + 1)),
('Mid Autumn 37,', range(37, 1, +1)),
('Late Autumn ', range(38, 74, + 1)),
('Early Fall ', range(1, 37, + 1)),
('Mid Fall 37, ', range(37, 1, +1)),
('Late Fall ', range(38, 74, + 1))];
week = ['Mer', 'Ven', 'Ear', 'Mar', 'Jup', 'Sat', 'Ura', 'Nep', 'Plu']
start_pos = week.index(start_day)
def is_leap(year):
if (not year%4 and year%100 or not year%400): # algorithm to determine leap year
return True
if not is_leap(year):
del tup[3]
for month, days in tup:
# Print month title
print('{0} {1}'.format(month, year).center(20, ' '))
# Print Day headings
print(''.join(['{0:<3}'.format(w) for w in week]))
# Add spacing for non-zero starting position
print('{0:<3}'.format('')*start_pos, end='')
for day in days:
# Print day
print('{0!s:<3}'.format(day), end='')
start_pos += 1
if start_pos == 9:
# If start_pos == 9 (Sunday) start new line
print()
start_pos = 0 # Reset counter
print('\n')
start_day=(input('Enter start day of the year Mer,Ven,Ear,Mar,Jup,Sat,Ura,Nep,Plu'))
year=int(input('Enter Year'))
make_calendar(year, start_day)
this is a program that breaks down the calendar into equal units i think its cool but no matter what i do the tuple[3] does not work. please if someone can teach me more about tuples or why this isnt working i would be very greatfull. even if you dont know i appreciate you taking the time out of your day to read my problem.
The problem is likely related to this line of code:
year=int(input('Enter Year')),
The trailing comma means that year
is a tuple, not a plain integer.
If the function is_leap()
were ever called, then it would demonstrate the stated error behavior.
However, I must point out that the is_leap()
function is defined but it is never called, so I have to think that you haven't shown us the genuine code.
According to your post, this is the definition of is_leap()
:
def is_leap(year):
if (not year%4 and year%100 or not year%400): # **algorithm to determine leap year**
return True
if is_leap(year):
return False
del tup[3] #**it still prints 2019
As you can see, the call to is_leap()
is contained within the function itself, and since that is the only call to that function in the entire code, it is never called.
Perhaps this is an indentation error, and the if is_leap(year)
block is actually meant to be outdented one level?