Reputation: 31
I can't find the correct loops to use to make this code work!
I tried to use a while loop and I could get Python to show all the leap years between the two years selected by the user but not in the formatting that I was asked to use.
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
if start < end:
print ("Here is a list of leap years between " + str(start) + " and " + str(end) + ":")
while start < end:
if start % 4 == 0 and start % 100 != 0:
print(start)
if start % 100 == 0 and start % 400 == 0:
print(start)
start += 1
if start >= end:
print("Check your year input again.")
Problem description: A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it is also divisible by 400. Write a program which works out the leap years between two years given by the user. The program should list 10 leap years per line, with commas between each year listed and a full stop at the end, as in the following example input/output:
Enter start year: 1000
Enter end year: 1200
Here is a list of leap years between 1000 and 1200:
1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032, 1036, 1040,
1044, 1048, 1052, 1056, 1060, 1064, 1068, 1072, 1076, 1080,
1084, 1088, 1092, 1096, 1104, 1108, 1112, 1116, 1120, 1124,
1128, 1132, 1136, 1140, 1144, 1148, 1152, 1156, 1160, 1164,
1168, 1172, 1176, 1180, 1184, 1188, 1192, 1196, 1200.
Hints: the answer uses a for loop to work through all the years from the start year to the end year, an extra variable as a leap year counter, and various if and if-else statements inside the loop to check if the year is a leap year, if a comma is needed, and if a new line is needed.
Upvotes: 3
Views: 17270
Reputation: 1
Ok, you can use the calendar module, too. The other answers have already told you how to get and validate the start and end of the years, and how to print them out. And easier way to validate if a year is a leap year, is by using this module:
import calendar
leap_years = [i for i in range(start,end+1) if calendar.isleap(i)]
Upvotes: 0
Reputation: 1
for yr in range(399, 510):
if (yr%4==0):
if(yr%100==0):
if (yr%400==0):
print(f" leap year {yr}")
else:
print(f" leap year {yr}")
i think this is most easy way to print out all the leaps yrs in given range
Upvotes: 0
Reputation: 1
y = int(input("Enter year: "))
count=0
if y < 1600 :
print("check your year input again")
for i in range(1600,y):
if i % 400 == 0 and i % 100 != 0:
print(i, "is a Leap Year")
elif i % 4 == 0 :
count += 1
print(i, "is a Leap Year.")
print("Total leap year: {}".format(count))
Upvotes: -1
Reputation: 34046
Try it:
while start < end:
if start % 4 == 0:
if start % 100 == 0:
if start % 400 == 0:
print("{} is a leap year".format(start))
else:
print("{} is not a leap year".format(start))
else:
print("{} is a leap year".format(start))
else:
print("{} is not a leap year".format(start))
start += 1
Out:
Enter start year: 2000
Enter end year: 2020
2000 is a leap year
2001 is not a leap year
2002 is not a leap year
2003 is not a leap year
2004 is a leap year
2005 is not a leap year
2006 is not a leap year
2007 is not a leap year
2008 is a leap year
2009 is not a leap year
2010 is not a leap year
2011 is not a leap year
2012 is a leap year
2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year
2017 is not a leap year
2018 is not a leap year
2019 is not a leap year
[NOTE]:
for start in range(start, end)
instead of the while start < end
and start += 1
Upvotes: 0
Reputation: 79228
obtaining leap years:
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
list(range(start + (4 - start % 4), end + 1, 4))
Upvotes: 3
Reputation: 16952
I'm afraid I disagree with the hint that you use a counter to determine whether to print a comma or a full stop followed by a newline. It makes for a very complicated loop. The problem arises with the last line, which might have fewer than 10 year numbers in it. I reckon you still want a full stop not a comma at the end of that line.
Instead of printing your line inside a loop, build a table of years and format it afterwards. I've changed your solution as little as possible. One change that you did not ask for involved fixing the input validation.
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
while start >= end:
print("Check your year input again.")
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
print ("Here is a list of leap years between {0} and {1}:".format(start,end))
leap_years = []
while start <= end:
if start % 4 == 0 and start % 100 != 0:
leap_years.append(str(start))
if start % 100 == 0 and start % 400 == 0:
leap_years.append(str(start))
start += 1
for line in range(0, len(leap_years), 10):
print ("{0}.".format(", ".join(leap_years[line:line+10])))
Upvotes: 1
Reputation: 29
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
if start > end:
print("Check your year input again.")
else:
print ("Here is a list of leap years between " + str(start) + " and " + str(end) + ":")
while(start <= end):
if start % 100 == 0 and start % 400 == 0:
print(start)
start += 1
if start % 4 == 0 and start % 100 != 0:
print(start)
start += 1
else:
start += 1
You need to add else part. Because program can't do anything when there is not a leap year.
Upvotes: 0
Reputation: 912
As the hint says, use a counter. This counter should count the number of leap years you have printed. Every tenth leap year, you end the output with a newline, otherwise with comma and space.
To get the numbers right, just add a variable leap_year_counter
and check if ==10
and reset or %10
, then you can also output how many you found at the end.
To actually print with the correct ending, use the end
keyword argument of the print
function (print(value, end=<end you want>)
.
The for
loop part of the hint probably expects you to use for year in range(start, stop)
instead of the while
loop.
Upvotes: 0
Reputation: 180
Here is an elegant solution I thought might help you out:
start = int(input("Enter start year: "))
end = int(input("Enter end year: "))
if start <= end:
leap_years = [str(x + start) for x in range(end-start) if x % 4 == 0 and x % 100 != 0]
leap_years[-1] += "."
print(f"Here is a list of leap years between {start} and {end}:\n{(', '.join(leap_years))}")
else:
print("Check your input years again!")
An explanation for this? Essentially it makes a range between your start and end year and loops over it to see if it's divisible by 4 and NOT divisible by 400, if so; it adds it to an array that we can join using ,
and then add a period at the end and display this to the user using f-strings.
Upvotes: 0
Reputation: 6181
The condition should be different -
if (start % 4 == 0 and start % 100 != 0) or (start % 4 == 0 and start % 400 == 0):
Also in order to include the end year in the range the loop condition should be -
while start <= end:
Upvotes: 2