Oct-16-2020, 10:15 PM
hi there
I have just started teaching myself python.
I wanted to create a small bit of code that would include a function that would take a list of years , and for each of the years it would calculate if it was a leapyear based on the below algorithm:
"
The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years, if they are exactly divisible by 400.
"
So I have set the code to populate a list with the years 2000 to 2080. And my hope was that when this list was passed to my function, my function would create another list named truefalse that contained values of True or False. True corresponding to a leapyear and False corresponding to a non-leapyear.
And at the end of the function, I just wanted to return this truefalse list back the main code and have it printed.
Given the years that I had chosen, I would expect that the first value in the truefalse list would be True, and then followed by every 4th value being True.
However, when I run the code, the print at the end is just returning a blank list. can someone please explain why my function is not returning the list named truefalse back to the main code as I had expected?
Full code below:
def leapyear(year):
truefalse=[]
while i in range(len(year)):
if not year[i]%4 and year[i]%100:
truefalse.append(True)
elif not year[i]%4 and not year[i]%100:
if not year[i]%400:
truefalse.append(True)
else:
truefalse.append(False)
return truefalse
yearlist=[]
for i in range(2000,2081):
yearlist.append(i)
print(leapyear(yearlist))
I have just started teaching myself python.
I wanted to create a small bit of code that would include a function that would take a list of years , and for each of the years it would calculate if it was a leapyear based on the below algorithm:
"
The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years, if they are exactly divisible by 400.
"
So I have set the code to populate a list with the years 2000 to 2080. And my hope was that when this list was passed to my function, my function would create another list named truefalse that contained values of True or False. True corresponding to a leapyear and False corresponding to a non-leapyear.
And at the end of the function, I just wanted to return this truefalse list back the main code and have it printed.
Given the years that I had chosen, I would expect that the first value in the truefalse list would be True, and then followed by every 4th value being True.
However, when I run the code, the print at the end is just returning a blank list. can someone please explain why my function is not returning the list named truefalse back to the main code as I had expected?
Full code below:
def leapyear(year):
truefalse=[]
while i in range(len(year)):
if not year[i]%4 and year[i]%100:
truefalse.append(True)
elif not year[i]%4 and not year[i]%100:
if not year[i]%400:
truefalse.append(True)
else:
truefalse.append(False)
return truefalse
yearlist=[]
for i in range(2000,2081):
yearlist.append(i)
print(leapyear(yearlist))