Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop stumbling block
#1
Hey guys
I am busy with a task that is teaching me on how to use for loops which I need a little help.
Basically I need to use a for loop + if statements and:

● Write a program to input a year and a number of years. 
● Then determine and display which of those years were or will be leap  years. 

 
What year do you want to start with? 1994 
How many years do you want to check? ​8   

1994 isn’t a leap year 
1995 isn’t a leap year 
1996 is a leap year 
1997 isn’t a leap year 
1998 isn’t a leap year 
1999 isn’t a leap year 
2000 is a leap year 
2001 isn’t a leap year

I feel like my code is very close, I get the leap years displayed but my if statement for displaying the normal years as in the example above does not want to work, I only manage to get the leap years displayed. I am n total noob so its likely a very silly and obvious mistake.

If you can help, please explain as if you are talking to a 6 year old as I have 5days of coding experience in total.


Here is my code:


start = int(input("Which year to start on? "))
amount = int(input("How many years to check? "))
total = start + amount + 1
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
        if years %4 != 0:
            print(years, " This is not a leapyear.")
    
    
Thanks in advance for any assistance
Reply
#2
You need to dedent the last if clause
from this
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
        if years %4 != 0:
            print(years, " This is not a leapyear.")
to this:
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
    if years %4 != 0:
        print(years, " This is not a leapyear.")
As currently writen. The second if clause never executes as it is under the opposite condition's if clause causing it to never fire.

Aside from that you only need an else clause alone. If a year is not a leap year, then the only other option is it is not a leap year. So hence you only need an else clause instead of a second if clause. Even if you were to check something else, it should be an elif clause, not two if's one after the other like that.
like this:
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
    else:
        print(years, " This is not a leapyear.")
Recommended Tutorials:
Reply
#3
(Sep-10-2019, 09:06 PM)metulburr Wrote: You need to dedent the last if clause
from this
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
        if years %4 != 0:
            print(years, " This is not a leapyear.")
to this:
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
    if years %4 != 0:
        print(years, " This is not a leapyear.")
As currently writen. The second if clause never executes as it is under the opposite condition's if clause causing it to never fire.

Aside from that you only need an else clause alone. If a year is not a leap year, then the only other option is it is not a leap year. So hence you only need an else clause instead of a second if clause. Even if you were to check something else, it should be an elif clause, not two if's one after the other like that.
like this:
for years in range (start, total):
    if years %4 == 0:
        print(years, " This is a leapyear.")
    else:
        print(years, " This is not a leapyear.")

Ah yes that makes total sense, Thanks a stack MetulBurr your prompt and thorough response is deeply appreciated.
Reply
#4
Now is the right time to apply some critical thinking. This code ain’t determining leap years. It determines years divisible by four. Leap year is something different:

Quote: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. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.
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
#5
(Sep-11-2019, 05:59 AM)perfringo Wrote: Now is the right time to apply some critical thinking. This code ain’t determining leap years. It determines years divisible by four. Leap year is something different:

Quote: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. For example, the years 1700, 1800, and 1900 are not leap years, but the years 1600 and 2000 are.

Thanks for in interesting info perfringo , now I can lecture about leap years too Cool
Reply
#6
(Sep-11-2019, 07:20 AM)YoungGrassHopper Wrote: Thanks for in interesting info perfringo , now I can lecture about leap years too Cool

This knowledge is 'needed' in assignments because there are several sort of limitations (can't use built-in modules etc). 'Normal' way of doing it would be using built-in modules like calendar etc.

If it's a homework then there are (probably) automated tests which (probably) will use built-in modules which return correct leap years. So code will not pass if leap years are not calculated correctly. Professors also very often try to mislead students by providing test sample which seems general but actually is specific case (as with year 2000 in this example). That's why I called it 'critical thinking'. Been there, done that Smile
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
#7
(Sep-11-2019, 08:08 AM)perfringo Wrote:
(Sep-11-2019, 07:20 AM)YoungGrassHopper Wrote: Thanks for in interesting info perfringo , now I can lecture about leap years too Cool

This knowledge is 'needed' in assignments because there are several sort of limitations (can't use built-in modules etc). 'Normal' way of doing it would be using built-in modules like calendar etc.

If it's a homework then there are (probably) automated tests which (probably) will use built-in modules which return correct leap years. So code will not pass if leap years are not calculated correctly. Professors also very often try to mislead students by providing test sample which seems general but actually is specific case (as with year 2000 in this example). That's why I called it 'critical thinking'. Been there, done that Smile

Thanks for the advice I really appreciate it perfringo. Its makes a whole lot of sense, I too think its important to maintain a critical thinking mindset.

So to get back to my piece of code , in order to make it technically sound I will need to print a phrase that states: This program will give a false positive on all centurial years which are NOT exactly devisable by 400.
Because I have no clue how to work such a fail safe into the code. My coding toolbox is vary sparse still.
I know about the "or" function but I think that only allows me to enter one condition for example:

if (years %4 == 0)or(years%400 == 0):
But then the centurial years divisible by 100 is still going to produce a false positive and I do not know to to account for that in the code apart from printing a phrase that states they(centurial years exactly divisible by 100) will produce a false positive.


Even if I could attach another "or" I am not quite sure how to state it. I think I need an "Unless" function next to the "or" LOL

Do you perhaps have a suggestion?
Reply
#8
I like Python a lot because you can almost directly translate spoken language into it.

Lets observe objective: "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 'every year that is exactly divisible by four':

year % 4 == 0
'except for years that exactly divisible by 100':

year % 100 != 100
'but these centurial years are leap years if they are exactly divisible by 400'

year % 400 == 0
And together:

if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
EDIT: some explanation about order

Python first evaluates 'and': if year is divisible by four and it does not end with 00. If so - it's leap year (True), however, if it evaluates to False on years ending with 00 then 'or' clause is executed which checks whether year is divisible by 400. This way 1900 is False and 2000 is True.
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
#9
Wow , yes! I actually am familiar with both the and + or function/condition but I did not know they can be used together in one line like that, thank you very much for assisting I do appreciate it lots perfringo, problem solved above and beyond
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stop/continue While loop block Moris526 68 24,820 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  while loop stumbling block YoungGrassHopper 5 3,231 Sep-09-2019, 08:36 PM
Last Post: YoungGrassHopper

Forum Jump:

User Panel Messages

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