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
#2
(Oct-16-2020, 10:15 PM)sudonym3 Wrote: 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))

unfortunately it looks like when I posted this initial post, all the indentation in my code was lost. Sorry about that! Does anyone know how I avoid losing the indentation for future reference?
Reply
#3
Use the "Insert Python" icon, it's the first one on the second row of icons.
Reply
#4
(Oct-16-2020, 10:56 PM)nhl66pens Wrote: Use the "Insert Python" icon, it's the first one on the second row of icons.

Thanks, I have now re-inserted my code with the indentation intact - see below. (also, I found a bug in my leapyear algorithm which I corrected since I posted the original code!)

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)
		else:
			truefalse.append(False)


	return truefalse

yearlist=[]

for i in range(2000,2081):
	yearlist.append(i)



print(leapyear(yearlist))
Reply
#5
You can fix the formatting of your post by putting the code in BB "python" tags.

But the big problem is with this line:
    while i in range(len(year)):
I think you meant something like:

    for i in range(len(year)):
You've confused while and for here. That said, this method of doing indirect access by looping over the positions isn't necessary. Better would be direct access such as:

def leapyear(yearlist):
    truefalse=[]
    for year in yearlist:
        if not year % 4 and year % 100:
        ...
You don't need a range, you don't need to keep track of the index, etc...
sudonym3 likes this post
Reply
#6
There is shorter and arguably simple way to determine leap year:

if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:


Python first evaluates 'and' (which is first part of 'or' clause): is year divisible by four and not divisible by 100. If so - it's leap year (True). However, if 'and' evaluates to False second part of 'or' clause is evaluated which checks whether year is divisible by 400 (if year is divisible by 400 it is divisible by four as well)
sudonym3 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 177 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Need help with sorting lists as a beginner Realist1c 1 745 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,708 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Waiting for heavy functions question philipbergwerf 14 3,372 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
Question Beginner Boolean question [Guessing game] TKB 4 2,309 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Beginner question NameError amazing_python 6 2,447 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,213 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  Noob question about lists adifrank 4 2,879 Nov-19-2020, 03:26 AM
Last Post: adifrank
  Question about functions(built-in) Jinja2Exploitation 2 1,941 Nov-15-2020, 02:13 PM
Last Post: jefsummers
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,372 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