Python Forum
Algorithm for leap year (structogramm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm for leap year (structogramm
#1
Photo 
Hello!

Before I solve a problem I usually always draw a structogramm and then start to code. But I just can´t find a way how to code
this structogramm. I have the feeling that I´m doing something wrong with the if-statements

I would be very happy if you could code exactly this structogramm attached to this thread and show me how you would do it!

Thanks
SW

This is the structogram: https://ibb.co/NKtMLrC


leap = 0

year = int(input("Please type in your year :"))


if year%4 == 0:
    pass
    if year%100 == 0:
        pass
    else:
        leap = 1

        if year%400 == 0:
            leap = 1
        else:
            pass

if leap == 1:
    print("This year is a leap year")

else:
    print("This year isn´t a leap year")
Reply
#2
One way to look at this problem: define two cases for leap year and use or to stitch them together:

- divisible by 4 and not divisible by 100 -> leap years, but misses years divisible by 400
- divisible by 400 -> leap years missed in first case

In Python code it can be expressed something like below, returns True if leap year, False if not; if first case is true then or condition will not be evaluated, if first case is false then or kicks in and result of second case evaluation will be returned:

year % 4 == 0 and year % 100 != 0 or year % 400 == 0
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
#3
Thanks a lot! This is big help
Reply
#4
You can use calendar.isleap(year).

In [1]: import calendar

In [2]: calendar.isleap(2020)
Out[2]: True
And this is the implementation:
In [3]: calendar.isleap??
Signature: calendar.isleap(year)
Source:   
def isleap(year):
    """Return True for leap years, False for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
File:      ~/.pyenv/versions/3.9.6/lib/python3.9/calendar.py
Type:      function
coder_sw99 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get year not the entire year & time mbrown009 2 862 Jan-09-2023, 01:46 PM
Last Post: snippsat
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Leap Year Issue spalisetty06 3 2,173 Jul-02-2020, 03:29 PM
Last Post: bowlofred
  Leap Year MartinEvtimov 14 24,947 Mar-10-2017, 01:46 AM
Last Post: Larz60+
  leap year program issue jashajmera 3 3,824 Feb-04-2017, 11:51 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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