javierkid javierkid - 1 month ago 20
Python Question

Using the List Feature in Python

month_days= [('January',[31]),('February',[28,29]),('March',[31]),

('April',[30]),('May',[31]),('June',[30]),('July',[31]),('August',[31]),

('September',[30]),('October',[31]),('November',[30]),('December',[31]) ]

day_names =
['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

Problem 1
Write a function days_in_month which takes a month as an argument and finds the
corresponding month in the month_days list and returns the number of days associated with that
month.
Im open for explanations aswell

Answer
def days_in_month(month):
    for m, nblist in month_days:
        if month==m:
            return nblist
    else:
        return []


days_in_month('May')
Out[20]: [31]

days_in_month('Mady')
Out[21]: []