help me in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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")

30th Aug 2021, 12:47 PM
Alireza Azizzadeh
Alireza Azizzadeh - avatar
4 Answers
+ 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.
30th Aug 2021, 2:52 PM
Python Learner
Python Learner - avatar
+ 2
Works fine on sololearn as well.
30th Aug 2021, 12:50 PM
Abhay
Abhay - avatar
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"?
16th Sep 2021, 10:52 AM
Adam Sturrock
Adam Sturrock - avatar
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.
18th Sep 2021, 2:32 PM
Python Learner
Python Learner - avatar