0

# Elif — Leap Year Practice

What did I do wrong ? #5 and #6 case didn’t check through year = int(input()) if year % 4 : print("Leap year") elif year % 100 : print("Leap year") elif year % 400 : print("Not a leap year") else : print("Leap year")

3 ответов

+ 5

Firstly, you need to remember that if you say year % 4, you will get the remainder of the operation. If you wanted to know if the number is divisible by 4, you should do year % 4 == 0 to know if there was not a remainder.
Secondly, when the if statement checks if it is divisible by 100, it will output "Not leap year" and then the program would be over omitting the part when it checks if it is divisible by 400.
A better approach to your problem would be the following:
year = int(input())
condition1 = year % 4 == 0
#This variable will be True if the year is divisible to 4
condition2 = not year % 100 == 0 or year % 400 == 0
#This variable will be True if the year is not divisible by 100 or if the year is divisible by 400
if condition1 and condition2:
print("Leap year")
else:
print("Not leap year")
#If both conditions are true, output Leap year, otherwise output Not leap year

+ 2

According to the following article ,
https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year
It seems like you need to check first,
if year %100 and year%400 ,then leap year
elif year%4 ,then leap year
else not leap year.

0

Pls someone help paste the leap year question here.