+ 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")
11 RĂ©ponses
+ 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")
+ 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")
+ 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
+ 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")
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")
0
thanks get !! this one worked!!
0
Daniel Lopez, Simba's code is also working (our codes do the same, but in different ways)!
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.
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")
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")
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")