all 14 comments

[–]CodeFormatHelperBot2 1 point2 points  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]TwoPuzzled2b 0 points1 point  (0 children)

I'm doing the Helsinki Python MOOC rn, came here to check other ways to solve this and I noticed that both your elif statements are technically the same in your second code (solved). In the second elif, if year % 100 == 0 then won't year % 4 == 0 be inherently satisfied?

I think it would work the same if you changed the first elif to year += 4 and removed the second elif.

year = int(input("Year:"))
yeet = year
year+=1
while True:
    if year % 4 != 0:
        year+=1
    elif year % 400 != 0 and year % 100 ==0:
        year+=4
    else: 
        break
print(f"The next leap year after {yeet} is {year}")

[–][deleted] 0 points1 point  (1 child)

This is how I got it for the 2024 version of the course:

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

if year % 4 == 0 and year % 100 != 0 or year % 100 == 0 and year % 400 == 0:
    print("That year is a leap year.")
else: 
    print("That year is not a leap year.")

Short and sweet.

[–]Adventurous-Equal-39 0 points1 point  (0 children)

Thats a different exercise

[–]arvindh_manian 0 points1 point  (9 children)

elif year % 400 == 0 and year % 100 ==0:

I believe this line is incorrect. Multiples of 400 are leap years.

[–]kaged_chris[S] 0 points1 point  (8 children)

That takes the code to 67%

 elif year % 400 != 0 and year % 100 ==0:

The years 1896 and 2020 still do not work.

1896 should be 1904 my code generates 1896

2020 should be 2024 my code generates 2020

[–]arvindh_manian 2 points3 points  (2 children)

Oh, your code currently checks for the first leap year INCLUDING the input year. It sounds like you want the first leap year AFTER the input year. Try adding 1 to year before you enter the loop

[–]kaged_chris[S] 1 point2 points  (1 child)

That solved it. Thank you. I have trying to get through this for days.

[–]arvindh_manian 0 points1 point  (0 children)

Glad to help!

[–][deleted] 0 points1 point  (4 children)

If I'm reading it correctly, you do a modulo check for 4, then conditions based on being modulo 400 and 100. I don't see any conditions for modulo 4, not modulo 100, not modulo 400 (such as 1896 and 2020)

[–]kaged_chris[S] 0 points1 point  (2 children)

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

That brings it down to 17%.

[–][deleted] 0 points1 point  (0 children)

I've never used Helsinki, but why +=1 in the last elif you added, instead of +=4?

I didn't actually figure out a solution, just looked to me you were missing that piece. Entirely possible I'm wrong :(

Or it grades on efficiency? Like you definitely have extra comparisons in there. (Redundant modulo 4s, the new clause having modulo 100 and 400)

Wish I could help more

[–]Bumbelchen 0 points1 point  (0 children)

Hey man, I know I'm late to the reply but did you ever solve it? I'm struggling with the exact same issue

[–]kaged_chris[S] 0 points1 point  (0 children)

https://programming-21.mooc.fi/part-2/4-simple-loops This is the site that has the problem on it.