Python Forum
Tracking leap.py years for gregorian calendar (Exercism org)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tracking leap.py years for gregorian calendar (Exercism org)
#11
(A leap year is divisible by 4) and (a leap year is not divisible by 100 or a leap year divisible by 400).
Natural language is unsuitable for programming tasks because ambiguities and inaccuracies are possible.
I think it's still possible to understand this wrong.

The other problem were many newcomers are struggling with, are expressions in if-conditions.
if wants to have a Boolean. If it's not a boolean, then it's converted to a boolean.
A precise description of this is here: https://docs.python.org/3/library/stdtyp...ue-testing


some_value = 42
by4 = 4
by3  = 3

# bool(0) == False
# bool(-1) == True
# bool(1) == True

# if by4, will call implicit if bool(by4), because by4 is not a boolean.
# if requires a True or False
# if an object is of the type Boolean,
# then bool is called with the object, to get a boolean back


#          False                    or  (3)
if some_value % by4 == 0 or by3:
    print("Always True because by3 is 3 and bool(3) is True")
    print("The first expression is False")
    print("And the second expression is the bug")


# You want:
if some_value % by4 == 0 and some_value % by3 == 0:
    print("You can divide 42 by 3 and 42 by 4")
    # no, you can't!
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
If you look in Lib/datetime.py in your Python distribution, you’ll find this function, which I wrote decades ago :wink:.

def _is_leap(year):
    "year -> 1 if leap year, else 0."
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
Note that 2023 calendar printable and binds more tightly than or: the parentheses are vital there, although I’d use them (for clarity) even if they weren’t necessary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog codility leap sort variant MoreMoney 5 395 Apr-06-2024, 08:47 PM
Last Post: deanhystad
  Task calendar problem humanical 8 1,691 Sep-04-2023, 02:55 PM
Last Post: Pedroski55
  Calculate AGE in years Kessie1971 17 3,512 Apr-26-2023, 03:36 PM
Last Post: deanhystad
  Exercism with Python Gyga_Hawk 8 121,385 Mar-18-2022, 08:53 AM
Last Post: ndc85430
  Meltdown Mitigation (operators + conditionals) beware: Exercism potential spoiler Drone4four 5 2,632 Feb-21-2022, 08:49 AM
Last Post: Gribouillis
  Calendar program louienyy 2 4,970 Mar-30-2020, 01:21 PM
Last Post: louienyy
Sad [Learning] Calendar without modules or list KoFu 5 4,822 Sep-09-2019, 03:25 PM
Last Post: DeaD_EyE
  Problem with code to calculate weekday for leap years! Event 2 2,850 Dec-15-2018, 05:13 PM
Last Post: Event
  Calendar calculations frequency 10 5,598 Nov-13-2018, 07:34 PM
Last Post: frequency
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 9,973 Mar-26-2018, 04:44 AM
Last Post: PyMan

Forum Jump:

User Panel Messages

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