-2
case = 1

while case == 1: try: year = int(input("Enter year")) except: print("Invalid input")

if year < 9999 and year > -9999:
    if year > 5000 or year < -46 or year == 0:
        print("Year",year,"Invalid Year")
    else:
        if year >= -46 and year <= 1752:
            if year % 4 == 0:
                print("Year",year,"Julian Leap Year")
            else:
                print("Year",year,"Julian Non Leap Year")
        else:
            if year >= 1753:
                if year % 4 == 0:
                    if year % 100 == 0:
                        if year % 400 == 0:
                            print("Year",year,"Gregorian Leap Year")
                        else:
                            print("Year",year,"Gregorian Non Leap Year")
                    else:
                        print("Year",year,"Gregorian Leap Year")
                else:
                    print("Year",year,"Gregorian Non Leap Year") 
else:
    print("goodbye!")
    case = 0

Basically, my question is, is this a good programming style? Is there a more efficient way to do this rather than a bunch of if statements or is programming completely dependent on how you see it flow? Everything works correctly so disregard any output or context. Purely asking about the format.

tluke
  • 1
  • 3
    As your code is working, your question is probably much better suited for https://codereview.stackexchange.com/ - it's too broad and off-topic for SO. – Thierry Lathuille Mar 06 '21 at 22:52

1 Answers1

0

How Thierry told, your question is better in SE Code Review but... here is one aswner with short code example:

Your code works, but isnt that good, you can use inline ifs and logical operators to shorten it. See:

if -9999 >= year >= 9999: # If year is between -9999 and 9999
    calendar = "Julian" if -46 <= year <= 1752 else "Gregorian" # Guess calendar checking if year is older than 1752, like in your code
    # Three conditions of Leap years
    fc = year % 4 == 0
    sc = year % 100 == 0
    tc = year % 400 == 0
    leap = "Leap Year" if (fc or (fc and sc and tc)) else "Non Leap Year" # Check if year matches just First Condition or all conditions
    print("Year", year, calendar, leap) # Print results
else:
    print("Goodbye!")
    case = 0 # Stop loop by using case

And here is the tests:

Year: 2017
Year 2017 Gregorian Non Leap Year
Year: 2020
Year 2020 Gregorian Leap Year
Year: 1689
Year 1689 Julian Non Leap Year
Year: 1660
Year 1660 Julian Leap Year

Also i replaced x > min and x < max operations with min <= x <= max, more info here

Hope it helps. Happy coding!

Edit: Added Invalid Year alert

if year < 9999 and year > -9999:
    calendar = "Julian" if -46 <= year <= 1752 else "Gregorian"
    fc = year % 4 == 0
    sc = year % 100 == 0
    tc = year % 400 == 0
    leap = "Leap Year" if (fc or (fc and sc and tc)) else "Non Leap Year"
    log = f"Year {year} {calendar} {leap}" if -46 <= year <= 5000 or year == 0 else f"Year {year} Invalid Year"
    print(log)
else:
    print("Goodbye!")
    case = 0
Sandmiz
  • 78
  • 5