+ 1
what is wrong with this code ?
determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False. def leapYear(year): if year % 4 == 0: return True elif year % 100 == 0: return False elif year % 400 == 0: return True year = int(input()) print(is_leap(year))
10 Respostas
+ 4
Last line of code must be like
print(leapYear(year))
and if u execute it
And those two elif conditions will only be true when if condition gets true means those two elif condition will never get executed and program will terminate executing first condition always if condition is true.
Reverse those conditions of if statements.
+ 1
details of the task :
An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.
In the Gregorian calendar, three conditions are used to identify leap years:
The year can be evenly divided by 4, is a leap year, unless:
The year can be evenly divided by 100, it is NOT a leap year, unless:
The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.
+ 1
# from codegolf:
def c(s):return s%16*(s%25<1)<(s%4<1)
+ 1
I tried with new code but still appears to me a message that there is some tasks gone wrong and I don't know why
def is_leap(year):
if year % 4 == 0 and year % 100 != 0 and year % 400 == 0:
return True
else:
return False
year = int(input())
print(is_leap(year))
+ 1
The code is wrong
try this one and you will see where is the issue
def leapYear(year):
if year % 4 == 0:
return 4
elif year % 100 == 0:
return 100
elif year % 400 == 0:
return 400
year = int(input())
print(leapYear(year))
And this is correct code(I think so). Check it out
2017 is not a leap year
1900 is a not leap year
2012 is a leap year
2000 is a leap year
def leapYear(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return "leap"
else:
return "NOT leap"
year = int(input())
print(leapYear(year))
+ 1
The conditions for a leap year in a Gregorian Calendar is in a reverse order to what you put up there:
It should be:
1. If a year is divisible by 400, it is a leap year.
2. If a year is divisible by 100 and not by 400, it is not a leap year.
3. If a year is divisible by 4 and not by 100, it is a leap year.
4. Every other years are not leap year.
Then the code should be:
def leapYear(year):
if year % 400 == 0:
return True;
elif year % 100 == 0:
return False;
elif year % 4 == 0:
return True;
else:
return False;
year = int(input('Enter the year: '));
print(leapYear(year));
+ 1
Here is the code using the steps on the problem description
year = int(input())
#your code goes here
def leapyear(year):
if year % 4 != 0:
return "Not a leap year"
if year % 100 == 0:
if year % 400 != 0:
return "Not a leap year"
return "Leap year"
print(leapyear(year))
0
The test if year % 4 will be true for some values for example, but the test for year % 100 will not be done so a value returns a false positive. Nesting if's is an option or using multiple tests.
0
Can you please explain to me the logic behind doing the if statements in other order ?
0
Look at your first test if the year is divisible by 4 the function returns true and no further testing is done. So for example, the year 2000 is divisible evenly by 4, but it is also divisible by 100 which means the return value should be false, but it is never checked