+ 1

leap year-don't know why my code is wrong.

To check whether a year is a leap year or not, you need to check the following: 1) If the year is evenly divisible by 4, go to step 2. Otherwise, the year is NOT leap year. 2) If the year is evenly divisible by 100, go to step 3. Otherwise, the year is a leap year. 3) If the year is evenly divisible by 400, the year is a leap year. Otherwise, it is not a leap year. ******** year = int(input()) #your code goes here if year%4==0 and year % 100==0 and year % 400==0: print('Leap year') else: print('Not a leap year')

1st Jun 2021, 5:52 PM
LI HAN
LI HAN - avatar
15 Réponses
+ 12
#try this year = int(input()) #your code goes here if year%4==0 and year % 100!=0 or year % 400==0: print('Leap year') else: print('Not a leap year')
1st Jun 2021, 6:13 PM
Simba
Simba - avatar
+ 3
You don't need nested if statements. You just need to rearrange them and change to not divisible by 100 and 1 to an or . If year is divisible by 400 or (year is divisible by 4 and year is not divisible by 100)
1st Jun 2021, 6:10 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Simba That's pretty much the same as what I'm saying, except rearranging them makes it more efficient due to short circuiting/cutting the AND/OR's
1st Jun 2021, 6:15 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Hi! try using nested branch operators, according to the description
1st Jun 2021, 5:56 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
LI HAN Yours is wrong, because it should be divisible by 4 but not by 100 unless it is divisible by 400. For instance 300 is not a leap year even though it is divisible by 4 (75) and also divisible by 100 (3), but it isn't divisible by 400 so it's not a leap year.
1st Jun 2021, 6:34 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ChaoticDawg I couldn't really understand the logic here. Thanks for the explain.
1st Jun 2021, 6:41 PM
LI HAN
LI HAN - avatar
+ 1
LI HAN that's because it's not python but C
1st Jun 2021, 6:54 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Tejas If it passed, then it will be purely circumstantial. Because it should be; if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) {} There is no condition in which; year % 400 == 0 && year % 100 != 0 Will return true. It is mathematically impossible for a number to both be divisible by 400 but not by 100! edit: Furthermore, I just copied the code and tested it myself, and it failed (says non-leap year is a leap year just because it is divisible by 4). It also gives a warning about the exact part of the code I am mentioning. I'm not trying to pick on you, I just figured that maybe you had made a typo when copying your code over.
1st Jun 2021, 7:01 PM
ChaoticDawg
ChaoticDawg - avatar
0
while I am trying, so many answers already. thank you. mine is still wrong, I will try yours. if year%4==0 and year % 100==0: print('Leap year') else: if year%400==0: print('Leap year') else: print('Not a leap year')
1st Jun 2021, 6:20 PM
LI HAN
LI HAN - avatar
0
#include <stdio.h> int main() { int year ; printf("Enter a year\n"); scanf("%d", &year); if(((year%4)==0)||((year%400)==0)&&((year%100)!=0)) { printf("%d is a leap year\n",year); } else { printf("%d is not leap year\n",year); } return 0; }
1st Jun 2021, 6:49 PM
Tejas Raut
0
For Leap Year year % 100 != 0
1st Jun 2021, 6:50 PM
Tejas Raut
0
Tejas the if condition in your code is incorrect. Take another look.
1st Jun 2021, 6:52 PM
ChaoticDawg
ChaoticDawg - avatar
0
Tejas Thanks. I haven't learned int main() yet. haha
1st Jun 2021, 6:52 PM
LI HAN
LI HAN - avatar
0
ChaoticDawg if condition is correct I have tested the code
1st Jun 2021, 6:56 PM
Tejas Raut
0
Litterally, boarding is better than coding, atleast THERE is an way OUT
3rd Jun 2021, 5:42 PM
Hugh