- 3
Kindly solve this complex code for me in python please mentioned in the discription experts .
5. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years unless they are also divisible by 400. Ask the user to enter a year, and, using the l/ operator, determine how many leap years there have been between 1600 and that year.
19 ответов
+ 1
we don't have to reinvent the wheel everytime. Use calendar.isleap()
import calendar
year = int(input())
# counter
n=0
# starting year
start = 1600
#if the input year is less than the start year, reverse the values
if start > year:
start, year = year, start
for y in range(start, year+1):
if calendar.isleap(y):
n+=1
print(n)
https://code.sololearn.com/cgwjQJ4IzcFD/?ref=app
+ 1
Brother couldn't tell in the output if the year is leap year or not.
+ 1
Example:
import calendar
year=1600
print(calendar.isleap(year))
>>True if it is a leap year
>>False if not.
Your original question was only about counting the number of leap years. calendar.isleap() takes care of determining if the year is a leap year or not.
It is trivial to add that functionality.
<edit>
Ok, I have added the print function for declaring if the input is a leap year or not.
+ 1
Thanks brother really appreciate your help
0
Does this is what you want ?
year = int(input())
leaps = 0
for i in range(min(1600, year), max(1600, year)) :
leaps += (1 if(not(i % 4) and (i % 100 or not(i % 400))) else 0)
print(leaps)
0
Nope
0
Why ?
0
Let me show you how I made
0
year = int(input("Enter year: "))
if year % 400 == 0 :
print(year, "is a Leap Year")
elif year % 100 == 0 :
print(year, "is not a Leap Year")
elif year % 4 == 0 :
print(year, "is a Leap Year")
else :
print(year, "is not a Leap Year")
print("Leap year between 1600 and that Year is :")
print("================================================")
for year in range(1600):
if year % 400 == 0 :
print(year, "==> is a Leap Year")
elif year % 100 == 0 :
print(year, "==> is not a Leap Year")
elif year % 4 == 0 :
print(year, "==> is a Leap Year")
else :
print(year, "==> is not a Leap Year")
0
This code has a problem
0
Working fine for me
0
It's running
0
Did you try with 2021 ?
0
Yes working 2021 is not a leap year
0
Of course it's running but try with 1610, the result should be 2
0
It's working paste it on your compiler.
0
Brother I have to determine whether the year is leap year or not that's the question the range is given 1600
0
Can't go above 1600 in this.
0
the first part of your code works fine,
but you also must determine how many leap years there have been between 1600 and that year