0
help me in this code
where is the problem? i enter this code in otherwebsites and they run it correctly, but sololearn says there is problem with else part year = int(input()) if year%4==0 : if year%100==0 : if year%400==0 : print ("Leap year") else : print ("Not a lip year")
4 ответов
+ 3
Hi EMOJIMAN!
It's giving no output for inputs like 2012 and 1700.
That's because, only your first if statement has an else statement to get a valid output for your inputs. It means the else is executed only if your first if statement is wrong. Inorder to handle all inputs you can add else statements to each if statements separately.
This is what I mentioned above.
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")
Keep in your mind that not all years that divided by 100 are leap years.
+ 2
Works fine on sololearn as well.
0
Hi, this looks to solve my problem too - thanks. Is there a reason why you need two more "else" statements after the first "not a leap year"?
0
Adam Sturrock these kinds of if-else statements are called nested if-else statements. In a nested statement, each if clause belongs to its else part which is in a same block. It means if any if statement evaluates false the program moves to its else statement and so on.
For example,
Let's say your input is 5. It won't be executed in first if statement since 5%4 == 0 is a false statement. So, it moves to the last else part. Similarly, you can think about other cases and check my previous answer once.