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