0

I'm working on a project that deals with dates. The user enters the month (in terms of a number - so "3", not "March") and a year (2005), and then it returns "March 2005 has 31 days. I'm nearly finished, but instead of returning the name of the month, it returns the number. So it says:

3 2005 has 31 days.

Here's my code:

def enteredMonth():
    month = int(input("Enter a month in terms of a number: "))
    if month == 1:
        month = "January"
    elif month == 2:
        month = "February"
    elif month == 3:
        month = "March"
    elif month == 4:
        month = "April"
    elif month == 5:
        month = "May"
    elif month == 6:
        month = "June"
    elif month == 7:
        month = "July"
    elif month == 8:
        month = "August"
    elif month == 9:
        month = "September"
    elif month == 10:
        month = "October"
    elif month == 11:
        month = "November"
    elif month == 12:
        month = "December"
    return month

def main():
    month = int(input("Enter a month in terms of a number: "))
    year = int(input("Enter a year: "))
    print(month, year, "has", numberOfDays(month, year) , "days")

How do I tweak the code so it returns the name of the month, not the number that the user entered?

Please help! I really appreciate it.

akaRem
  • 7,326
  • 4
  • 29
  • 43
user3105664
  • 179
  • 3
  • 6
  • 11

2 Answers2

1

You have your function, enteredMonth(), but never use it in your code.

print(enteredMonth(month), year, "has", numberOfDays(month, year), "days")

But a much better way to optimize enteredMonth is by simply using a dictionary:

months = {
    1: 'January'
    2: 'February'
    ...
}

Then you can access each month's string with its number, like so:

>>> months[1]
'January'
>>> months[4]
'April'
jayelm
  • 7,236
  • 5
  • 43
  • 61
0

Would something like this work?

def getmonthname:
    months = ['January', 'February', ... , 'December']
    return months[month - 1]
Michael Wheeler
  • 670
  • 7
  • 11