Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop stumbling block
#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


Messages In This Thread
for loop stumbling block - by YoungGrassHopper - Sep-10-2019, 08:38 PM
RE: for loop stumbling block - by metulburr - Sep-10-2019, 09:06 PM
RE: for loop stumbling block - by YoungGrassHopper - Sep-10-2019, 09:15 PM
RE: for loop stumbling block - by perfringo - Sep-11-2019, 05:59 AM
RE: for loop stumbling block - by YoungGrassHopper - Sep-11-2019, 07:20 AM
RE: for loop stumbling block - by perfringo - Sep-11-2019, 08:08 AM
RE: for loop stumbling block - by YoungGrassHopper - Sep-11-2019, 01:29 PM
RE: for loop stumbling block - by perfringo - Sep-11-2019, 02:45 PM
RE: for loop stumbling block - by YoungGrassHopper - Sep-11-2019, 03:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Stop/continue While loop block Moris526 68 26,210 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  while loop stumbling block YoungGrassHopper 5 3,380 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