In: Computer Science
Python 3.7.4:
The current calendar, called the Gregorian calendar, was introduced in 1582. Every year divisible by four was created to be a leap year, with the exception of the years ending in 00 (that is, those divisible by 100) and not divisible by 400. For instance, the years 1600 and 2000 are leap years, but 1700, 1800, and 1900 are not. Write a program that requests a year as input and states whether it is a leap year. We need to run this as many times as the user wants to check for leap years.
Below was marked incorrect for me. I need to put it in a while loop. Also, I'm curious if there is a way to do it without def? Do we have to call main? Sorry for all of the newbie questions. Thank you!
----------------------
def leap(year): if(year % 400 == 0): return True elif year % 100 == 0: return False elif year%4 == 0: return True else: return False def main(): year = int(input("Enter year: ")) if(leap(year)): print(year,"is a leap year") else: print(year, "is not a leap year") main()
Program code in python:
import sys #helps to use sys.exit to exit from the program
def leap(year): #function to check for leap year
if(year % 400 == 0):
return True
elif year % 100 == 0:
return False
elif year%4 == 0:
return True
else:
return False
def main(): #function to enter year
year = int(input("Enter year: "))
if(leap(year)):
print(year,"is a leap year")
else:
print(year, "is not a leap year")
replayMain()
def replayMain(): #function to get the choice to continue
ch = int(input("Do you want to continue: Press 1 to continue and 0
to exit.... "))
if(ch == 1):
main()
else:
sys.exit
main() #main function call
Screenshots attached below:
Screenshot of the python code:
Screenshot of sample output:
Please "Like" if this helped! Good luck!
Editted as per request to use something instead of sys.exit.....
You can also use break instead of sys.exit.
The code will be as :
def leap(year): #function to check for leap year
if(year % 400 == 0):
return True
elif year % 100 == 0:
return False
elif year%4 == 0:
return True
else:
return False
def main(): #function to enter year
year = int(input("Enter year: "))
if(leap(year)):
print(year,"is a leap year")
else:
print(year, "is not a leap year")
replayMain()
def replayMain(): #function to get the choice to continue
ch = int(input("Do you want to continue: Press 1 to continue and 0
to exit.... "))
while ch == 0:
break #exits the program
else:
main()
main() #main function call
code screenshot:
output:
Good luck!