+ 1

Help with the elif - leap year exercise

Can somebody tell me what mistake is there in the code to solve the leap year exercise? This is my code: year = int(input()) #your code goes here if year % 4 != 0: print ("Not a leap year") elif year % 100 != 0: print ("Not a leap year") elif year % 400 != 0: print ("Not a leap year") else: print ("Leap year")

16th Feb 2021, 3:49 PM
Daniel Lopez
11 Respuestas
+ 8
Check out the condition for leap year in the first case year = int(input()) # your code goes here if year % 4 == 0 and year % 100 != 0: print(year, "is a Leap Year") elif year % 100 == 0: print(year, "is not a Leap Year") elif year % 400 ==0: print(year, "is a Leap Year") else: print(year, "is not a Leap Year")
16th Feb 2021, 3:59 PM
Aditya
Aditya - avatar
+ 2
I was able to figure out the long version of it, if that helps at all. 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")
10th Mar 2021, 1:08 PM
Josh Lindsey
Josh Lindsey - avatar
+ 2
Took me a while to combine all these answers in the perfect one but here is what I've done myself year = int(input()) if year%4==0 and year%100!=0: print('Leap year') elif year%4==0 and year%100==0 and year%400==0: print('Leap year') elif year%4!=0 and year%100==0 and year%400!=0: print('Not a leap year') elif year%4!=0: print('Not a leap year') elif year%4==0 and year%100==0 and year%400!=0: print('Not a leap year') it is for every single case
18th Sep 2021, 9:29 PM
David Draghici
David Draghici - avatar
+ 2
it took me a minute to figure out the right method, but I managed to solve all test cases with: year = int(input()) if year % 4 != 0: print("Not a leap year") elif year % 100 != 0: print ("Leap year") elif year % 400 == 0: print ("Leap year") else: print ("Not a leap year")
20th Nov 2021, 1:51 AM
Tim
Tim - avatar
0
Second condition: elif year % 100 == 0: Instead of: elif year % 100 != 0: Full code: year = int(input()) #your code goes here if year % 4 != 0: print ("Not a leap year") elif year % 100 == 0: print ("Not a leap year") elif year % 400 != 0: print ("Not a leap year") else: print ("Leap year")
16th Feb 2021, 4:18 PM
#0009e7 [get]
#0009e7 [get] - avatar
0
thanks get !! this one worked!!
16th Feb 2021, 4:50 PM
Daniel Lopez
0
Daniel Lopez, Simba's code is also working (our codes do the same, but in different ways)!
16th Feb 2021, 4:52 PM
#0009e7 [get]
#0009e7 [get] - avatar
0
Simba, 1. It is normal that there are multiple ways to solve the same task. 2. Should be, and our codes should be like that.
16th Feb 2021, 5:09 PM
#0009e7 [get]
#0009e7 [get] - avatar
0
it would work :-) 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('Leap year') else: print("Not a leap year")
2nd Jun 2021, 7:38 PM
Neeraj Thakur
Neeraj Thakur - avatar
0
year = int(input()) #your code goes here if year%4==0 and year%100!=0 : print( "Leap year") elif year%4==0 and year%100==0 and year%400==0 : print( "Leap year") else : print("Not a leap year")
27th Jun 2021, 2:54 AM
KOW WAI NA
KOW WAI NA - avatar
0
year = int(input()) #your code goes here if year%4==0 and year%100!=0: print ("Leap year") elif year%400==0 : print ("Leap year") else: print ("Not a leap year")
7th Feb 2024, 6:35 AM
T LALTLANTHANGI
T LALTLANTHANGI - avatar