- 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.

2nd Dec 2021, 8:59 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
19 Respuestas
+ 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
4th Dec 2021, 3:19 AM
Bob_Li
Bob_Li - avatar
+ 1
Brother couldn't tell in the output if the year is leap year or not.
4th Dec 2021, 9:13 AM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
+ 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.
4th Dec 2021, 11:05 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks brother really appreciate your help
4th Dec 2021, 11:06 AM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
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)
3rd Dec 2021, 6:25 PM
VCoder
VCoder - avatar
0
Nope
3rd Dec 2021, 6:27 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
Why ?
3rd Dec 2021, 6:27 PM
VCoder
VCoder - avatar
0
Let me show you how I made
3rd Dec 2021, 6:27 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
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")
3rd Dec 2021, 6:30 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
This code has a problem
3rd Dec 2021, 6:41 PM
VCoder
VCoder - avatar
0
Working fine for me
3rd Dec 2021, 6:42 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
It's running
3rd Dec 2021, 6:42 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
Did you try with 2021 ?
3rd Dec 2021, 6:42 PM
VCoder
VCoder - avatar
0
Yes working 2021 is not a leap year
3rd Dec 2021, 6:43 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
Of course it's running but try with 1610, the result should be 2
3rd Dec 2021, 6:44 PM
VCoder
VCoder - avatar
0
It's working paste it on your compiler.
3rd Dec 2021, 6:44 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
Brother I have to determine whether the year is leap year or not that's the question the range is given 1600
3rd Dec 2021, 6:45 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
0
Can't go above 1600 in this.
3rd Dec 2021, 6:45 PM
SAfiullaH KhoKhar
SAfiullaH KhoKhar - avatar
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
3rd Dec 2021, 7:38 PM
VCoder
VCoder - avatar