Write a Python program \"daysInMonthXXXXX.py\" where XXXXX should be replaced by
ID: 3565925 • Letter: W
Question
Write a Python program "daysInMonthXXXXX.py" where XXXXX should be replaced by your ID. The program ask user to enter a accepts 2 inputs: a month and year. The program then calculates the number of days in this month of this year. The algorithm to determine if a year is a leap year or not can be found here: http://www.timeanddate.com/date/leapyear.html A year is leap if it is divisible by 400 (example 1600). A year is leap if it is divisible by 4 but not divisible by 100 (example 2012). A year is Not leap if it is divisible 4 and by 100 (example 1700). A year is Not leap if it is not divisible by 4(example 2013). Your program should display an error message when user enters a 0 or negative number for a year. A month also has to be from 1 through 12. Create a function isLearYear(year) and put code for leap year determination in this function. This function should return either true or false. Create a function daysInMonthYear(month, year) to do the calculation of days in month. This function returns the number of days. Main function should read the input, check the input values, print error message if inputs are not valid. If both inputs are good, then main will call the function daysInMonthYear(month, year). Examples of program run: This program was written by <your name> Please Enter a year: 2001 Please Enter a month: 2 There are 28 days in the month 2 of year 2001 ------------------------------------------- This program was written by <your name> Please Enter a year: 2000 Please Enter a month: 2 There are 29 days in the month 2 of year 2000 ------------------------------------------- This program was written by <your name> Please Enter a year: 1600 Please Enter a month: 2 There are 29 days in the month 2 of year 1600 ------------------------------------------- This program was written by <your name> Please Enter a year: 1700 Please Enter a month: 2 There are 28 days in the month 2 of year 1700 ------------------------------------------- This program was written by <your name> Please Enter a year: 2000 Please Enter a month: 1 There are 31 days in the month 1 of year 2000 ------------------------------------------- This program was written by <your name> Please Enter a year: 2000 Please Enter a month: 3 There are 31 days in the month 3 of year 2000 ------------------------------------------- This program was written by <your name> Please Enter a year: -2 Invalid year -2 ------------------------------------------- This program was written by <your name> Please Enter a year: 2000 Please Enter a month: 13 Invalid month 13
Explanation / Answer
Program Code:
#inputMonth method that accepts a month
def inputMonth():
month = int(input("Enter the month number: "))
return month
#inputYear method that accepts a year
def inputYear():
year = int(input("Enter a year: "))
return int(year)
#isleapYear method checks whether the give year is leap year or not
def isleapYear(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
else:
return False
#daysInaMonth method that calculates the days in a month
def daysInaMonth(month,year):
if month == 1:
return 31
elif month == 2:
if isleapYear(year):
return 29
else:
return 28
elif month == 3:
return 31
elif month == 4:
return 30
elif month == 5:
return 31
elif month == 6:
return 30
elif month == 7:
return 31
elif month == 8:
return 31
elif month == 9:
return 30
elif month == 10:
return 31
elif month == 11:
return 30
elif month == 12:
return 31
else:
return 0
#main method
def main():
month = inputMonth()
year = inputYear()
is_leapyear = isleapYear(year)
number_of_days = daysInaMonth(month,year)
print (month, year, "has", number_of_days, "days")
if is_leapyear:
print "The ", year , "is a leap year"
else:
print "The ", year , "is not a leap year"
if __name__ == '__main__':
main()
Sample Output:
Enter the month number: 2
Enter a year: 2012
2 2012 has 29 days
The 2012 is a leap year
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.