+ 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')
15 ответов
+ 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')
+ 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)
+ 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
+ 2
Hi! try using nested branch operators, according to the description
+ 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.
+ 1
ChaoticDawg
I couldn't really understand the logic here. Thanks for the explain.
+ 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.
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')
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;
}
0
For Leap Year year % 100 != 0
0
Tejas the if condition in your code is incorrect. Take another look.
0
Tejas
Thanks. I haven't learned int main() yet. haha
0
ChaoticDawg if condition is correct
I have tested the code
0
Litterally, boarding is better than coding, atleast THERE is an way OUT