+ 5

Python - Leap Year

Hi all, This does not bode too well but I have become stuck on the Leap Year Python practice test. Can you please let me know why the below code is not working? This one has got the better of me. year = int(input()) if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: print ("Leap year") else: print ("Not a leap year") Appreciate any help as to why that does not work rather than the answer to help better my understanding of if statements and how they can be used. Patrick

7th Feb 2021, 11:19 PM
Patrick Bennett
5 Antworten
+ 7
You are not covering every case Suppose year divisible by 4 but not by 100: nothing will be printed
7th Feb 2021, 11:33 PM
Angelo
Angelo - avatar
+ 3
Hey Patrick, I got stuck on this one too. In the lessons before this one I noticed that "print" was after every "if" statement. So I tried reverse engineering and using the != 0 instead of == 0 to output a print command. Here's what I came up with. I didn't see that step 2 said "IS a leap year" which stumped me for a while. year = int(input()) #your code goes here if year % 4 != 0: print("Not a leap year") elif year % 100 != 0: print("Leap year") elif year % 400 != 0: print("Not a leap year") else: print("Leap year")
15th Feb 2021, 9:25 PM
Loc T
Loc T - avatar
+ 2
Thanks all, just covering a couple of points. Leap years skip certain years which is there the 100 and 400 rules come into play. A Simple Guy, I have not covered and or statments so only had yo hand If/Else/elif Thank you for your help and all sorted using the below: year = int(input()) if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: print ("Leap year") else: print ("Not a leap year") else: print ("Leap year") else: print ("Not a leap year") Am guessing elif statments could have been used to save on the indentations.
8th Feb 2021, 8:58 PM
Patrick Bennett
0
You can use logical or for it ,otherwise you ca use elif also
8th Feb 2021, 5:00 AM
Atul [Inactive]
0
this was my final solution what kept messing me up was not reading the directions right step 2 says it is not divisible then you do print Leap year. year = int(input()) if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: print('Leap year') else: print('Not a leap year') else: print('Leap year') else: print('Not a leap year')
18th Jun 2021, 3:15 PM
Kevin Pallares