Python Forum
Search Results Web results Printing the number of days in a given month and year
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search Results Web results Printing the number of days in a given month and year
#1
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??
Reply
#2
Use next time this symbol to insert your code:    

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")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling methods and getting results out HerrAyas 4 299 Apr-02-2024, 09:37 PM
Last Post: deanhystad
  sort search results by similarity of characters jacksfrustration 5 443 Feb-16-2024, 11:59 PM
Last Post: deanhystad
  plotting based on the results of mathematical formulas Timur 1 347 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  get list of dates in past month, current and upcoming month jacksfrustration 4 564 Feb-03-2024, 06:37 PM
Last Post: deanhystad
  Updating sharepoint excel file odd results cubangt 1 836 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 769 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 715 Jun-22-2023, 06:04 PM
Last Post: jefsummers
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,446 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Trying to get year not the entire year & time mbrown009 2 888 Jan-09-2023, 01:46 PM
Last Post: snippsat
  Trying to send file to printer with no results. chob_thomas 2 3,380 Dec-21-2022, 07:12 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020