Python Forum
beginner question about lists and functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner question about lists and functions
#1
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))
Reply


Messages In This Thread
beginner question about lists and functions - by sudonym3 - Oct-16-2020, 10:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 227 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Need help with sorting lists as a beginner Realist1c 1 772 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,784 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Waiting for heavy functions question philipbergwerf 14 3,459 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
Question Beginner Boolean question [Guessing game] TKB 4 2,386 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,536 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,287 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  Noob question about lists adifrank 4 2,925 Nov-19-2020, 03:26 AM
Last Post: adifrank
  Question about functions(built-in) Jinja2Exploitation 2 1,978 Nov-15-2020, 02:13 PM
Last Post: jefsummers
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,433 Sep-19-2020, 09:12 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020