r/learnpython • u/bornlundi • 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 2024If 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 2028Please 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)}")
break19
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.
2
6
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}")
breakGet 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}")
break2
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
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
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 nThis is why programming languages have loops, do not incur the costs of a recursive function pathway
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.