0
Python else statement
Please help me to write a code for distinguish leap year or not. My understanding is here: year = int(input()) if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: print("Leap year") else: print("Not a leap year") When the input is 1900, expected outcome is Not a leap year, When the input is 2000, expected outcome is Leap year. Does my writing still have bugs?
4 ответов
+ 2
"""
ref : https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year
1) If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
2) If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
3) If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
4) The year is a leap year (it has 366 days).
5) The year is not a leap year (it has 365 days).
"""
year = int(input())
if (year%4==0): #1
if(year%100==0): #2
if(year%400==0): #3
print("leap year") #4
else:
print("not a leap year") #5
else:
print('not a leap year') #5
else:
print("not a leap year") #5
+ 2
Hi Yokos!
Whenever you try to test whether a year is a leap year or not, you need to think about one thing that is not all years that divided by 100 and 4 are leap years.
Here it is your working code.
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")
But you can make it even smaller by using logical operators.
https://code.sololearn.com/cKVMRHfemVW0/?ref=app
+ 2
Thank you for advices. I got it:)
+ 1
you can also combine all these ifs with and ..
so that code will look clean