Python Forum

Full Version: Search Results Web results Printing the number of days in a given month and year
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody!

I wrote this code to return the number of days for a given couple of months / day
my function must take into consideration leap years for the calculation of the number of days in the month of February
So my function should return none if the arguments don't make sense

This is the code
def isYearLeap(year):
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)

def daysInMonth(year, month):
   if month in ['September', 'April', 'June', 'November']:
        print 30

    elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
        print 31        

    elif month == 'February' and isYearLeap(year) == True:
        print 29

    elif month == 'February' and isYearLeap(year) == False:
        print 28

    else:
        return None
testYears = [1900, 2000, 2016, 1987]
testMonths = [2, 2, 1, 11]
testResults = [28, 29, 31, 30]
for i in range(len(testYears)):
	yr = testYears[i]
	mo = testMonths[i]
	print(yr, mo, "->", end="")
	result = daysInMonth(yr, mo)
	if result == testResults[i]:
		print("OK")
	else:
		print("Failed")[/quote]
NB:
at the beginning I did not enter the parentheses for the number of days (30/31/29/28) for the print function, I had this error
File "main.py", line 6
print 30
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print (30)?

*********************************


then I add the parentheses and I got this error
File "main.py", line 8
elif month in ['January', 'March', 'May', 'July', 'August', 'October', 'December']:


Can you help me please, how can i solve this issue??
Use next time this symbol to insert your code: [attachment=961]

print "something" is Python 2.7 syntax.
Don't use it! In Python 2.7 it's a statement, since Python 3 it's a function.

In addition, it's not a return statement.
If you want to return something from a function, then use return.

The next error is, that you try to look up an integer in a list, where only strings are.
Your testMonths are int. The items in the list are str.
This could not work.

Finally, an improved version of your code.
The first function is a code duplication from calendar.isleap.
The days_month is also in calendar.

days_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# or
# from calendar import mdays as days_month


def is_leap_year(year):
    return year % 4 == 0 and (year % 400 == 0 or year % 100 != 0)


def days_in_month(year, month):
    if month != 2:
        return days_month[month]
    elif month == 2:
        return days_month[month] + is_leap_year(year)
    raise ValueError("Illegal input")

# or
# from calendar import isleap as is_leap_year


if __name__ == "__main__":
    test_years = [1900, 2000, 2016, 1987]
    test_months = [2, 2, 1, 11]
    test_results = [28, 29, 31, 30]

    for year, month, result in zip(test_years, test_months, test_results):
        print(year, month, "-> ", end="")
        if result == days_in_month(year, month):
            print("OK")
        else:
            print("Failed")