r/learnpython • • Aug 05 '24

It's been 4 hours, what am i missing?

Something is wrong with my code on the mooc.fi Helsinki course for python this is the leap year question in part the 4th section of part 2 https://tmc.mooc.fi/paste/MeorB64S-YVuOjn6Jjso5w

the question is Please write a program which asks the user for a year, and prints out the next leap year.

Sample output

Year: 
2023
The next leap year after 2023 is 2024

If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:

Sample output

Year: 
2024
The next leap year after 2024 is 2028

Please write a program which asks the user for a year, and prints out the next leap year.Ć 

This is the solution i came up with

|| || ||*# Write your solution here*| ||year = int(input("Year: "))| || | ||while True:| ||if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):| ||print(f"The next leap year after {year} is {year + 4}")| ||break| ||elif year % 4 != 0 and (year + (4 - (year % 4))) % 100 == 0 and (year + (4 - (year % 4))) % 400 != 0:| ||print(f"The next leap year after {year} is {year + 4 + (year % 4)}")| ||break| ||else: | ||print(f"The next leap year after {year} is {year + 4 - (year % 4)}")| ||break|

UPDATE: GOT IT DONE TODAY, TOOK ANOTHER 20 MINUTES BUT WHO CARES IF ITS MORE CODE, JUST MAKE IT MORE CLEAR, I WILL SIMPLIFY LATER check it out here

# Write your solution here

year = int(input("Year: "))

while True: 
    ## covers 2020 1980 etc
    if year % 4 == 0 and (year + 4) % 100 != 0:
        print(f"The next leap year after {year} is {year + 4}")
        break
    ## covers 1896 1796
    elif year % 4 == 0 and (year + 4) % 100 == 0 and (year + 4) % 400 != 0: 
        print(f"The next leap year after {year} is {year + 8}")
        break
    ## covers 1596 1196, etc
    elif year % 4 == 0 and (year + 4) % 100 == 0 and (year + 4) % 400 == 0:
        print(f"The next leap year after {year} is {year + 4}")
        break
    ## covers 2019 2013 1010
    elif year % 4 != 0 and (year + (4 - (year % 4))) % 100 != 0:
        print(f"The next leap year after {year} is {year + (4 - (year %4))}")
        break
    ## covers 1897
    elif year % 4 != 0 and (year + (4 - (year % 4))) % 100 == 0 and (year + (4 - (year % 4))) % 400 != 0:
        print(f"The next leap year after {year} is {year + 8  - (year %4)}")
        break
    else: 
        print(f"The next leap year after {year} is {year + 4 - (year % 4)}")
        break
4 Upvotes

17 comments sorted by

17

u/[deleted] Aug 05 '24 edited Aug 05 '24

can you describe exactly what you mean when you say something is wrong, like is there and error, are there some inputs to the code that break it or give a wrong out put. In addition could you reformat the code you pasted in.

0

u/bornlundi Aug 05 '24

Specifically when I try to input the year 1896 I get a response of 1900 but I’m supposed to get a response of 1904

10

u/[deleted] Aug 05 '24

the issues, i might be wrong, is that once you have determined that the current year is a leap year you print out year+4 as the next leap year but it might be the case that year+4 may not be a leap year. In this case 1896+4= 1900 is not a leap year but your code prints it out as such. what I recommend you do, take this with a grain of salt, before you print out the next leap year check if that is also a leap year.

def is_leap(year):
    return (year %4 ==0 and year%100!=0) or (year %400 ==0)

use this function to check whether you year +4 is a leap year if so print else add another 4 and print.

I think there is a better solution out there but this is works for me.

2

u/bornlundi Aug 05 '24

Bro this was genius thano you!

1

u/bornlundi Aug 06 '24

Genius, thanks again

# Write your solution here

year = int(input("Year: "))

while True: 
    ## covers 2020 1980 etc
    if year % 4 == 0 and (year + 4) % 100 != 0:
        print(f"The next leap year after {year} is {year + 4}")
        break
    ## covers 1896 1796
    elif year % 4 == 0 and (year + 4) % 100 == 0 and (year + 4) % 400 != 0: 
        print(f"The next leap year after {year} is {year + 8}")
        break
    ## covers 1596 1196, etc
    elif year % 4 == 0 and (year + 4) % 100 == 0 and (year + 4) % 400 == 0:
        print(f"The next leap year after {year} is {year + 4}")
        break
    ## covers 2019 2013 1010
    elif year % 4 != 0 and (year + (4 - (year % 4))) % 100 != 0:
        print(f"The next leap year after {year} is {year + (4 - (year %4))}")
        break
    ## covers 1897
    elif year % 4 != 0 and (year + (4 - (year % 4))) % 100 == 0 and (year + (4 - (year % 4))) % 400 != 0:
        print(f"The next leap year after {year} is {year + 8  - (year %4)}")
        break
    else: 
        print(f"The next leap year after {year} is {year + 4 - (year % 4)}")
        break

2

u/VerdiiSykes Aug 05 '24

Try giving 1900 as the input and check your if statements' conditions šŸ‘€

1

u/Relative_Claim6178 Aug 05 '24

Am I missing something? This seems like it should be the expected output. 1896 was a leap year, and the next one would be 1900, not 1904, right? Or am I missing something?

4

u/Unlikely_Total9374 Aug 05 '24

What you're missing is that 1900 isn't a leap year

2

u/Relative_Claim6178 Aug 05 '24

Good call, my bad.

19

u/Strict-Simple Aug 05 '24

Well, what is wrong? I say the code is doing exactly what it was written to do. Also, format your code.

https://stackoverflow.com/help/how-to-ask

2

u/bornlundi Aug 05 '24

Ah! Thank you for the article!

6

u/[deleted] Aug 05 '24 edited Aug 05 '24

You will get more help if you format your code as you have been asked to. You can edit your original post. Takes about 20 seconds. The longer you leave code unformatted fewer people will bother looking at it.


Trying to combine the decision about what is a leap year and skipping forward X years to find the next one makes the code hard to read, so separate those two operations out. A simple approach is to write a function that decides if any particular year is a leapyear and use that in a loop that searches for the next leapyear. Something like

def is_leapyear(year):
    if (year % 100) == 0:
        # year is a century year, leapyear if multiple of 400
        return (year % 400) == 0
    # else a leapyear if multiple of 4
    return (year % 4) == 0

year = int(input("Year: "))
    
for i in range(8):
    year += 1
    if is_leapyear(year):
        print(f"next is {year}")
        break

Get that running first and test that it works. This alone may be enough to pass the test.

Then you could try to optimize by looking at the entered year and choosing possible candidates for the next leapyear depending on the year you give. For instance, given a year 1897 the next possible leapyear is 1897 + (4 - 1897%4) and the next after you just add another 4 as you have been doing. You can do that more easily like this:

year = <something>
next = year + (4 - year%4)
for candidate in (next, next + 4):
    if is_leapyear(candidate):
        print(f"next is {candidate}")
        break

2

u/beast_of_production Aug 05 '24

This course has a Discord where people are hanging out most of the time. You will get better answers there.

0

u/[deleted] Aug 05 '24 edited Aug 05 '24

Pretty easy to do with recursion

def isleap(year):
    if (year % 400 == 0) and (year % 100 == 0):
        return True
    elif (year % 4 ==0) and (year % 100 != 0):
        return True
    else:
        return False

def nextlpy(n):
    if isleap(n):
        return n
    return nextlpy(n+1)

# 2017 is not a leap year
# 1900 is not a leap year
# 2012 is a leap year
# 2000 is a leap year
for y in [2017, 1900, 2012, 2000]:
    print(y,isleap(y),nextlpy(y))

checking for leap year code is adopt from https://www.programiz.com/python-programming/examples/leap-year

8

u/[deleted] Aug 05 '24

easy to do with recursion

The OP is unlikely to know anything about recursion.

2

u/JohnnyJordaan Aug 05 '24

It's even easier to do without recursion, so why bother... also (year % 400 == 0) and (year % 100 == 0) has the needless second check, as every year divisible by 400 is divisible by 100 automatically.

def isleap(year):
    return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):

def nextlpy(n):
    while True:
        n += 1
        if isleap(n):
            return n

This is why programming languages have loops, do not incur the costs of a recursive function pathway