0

So I'm trying to write code to change an entered integer number into a month. The goal is to take an entered argument and convert it to a date. Code below.

def date(month, day, year):
    mon = ("January", "February", "March", "April", "May", "June", "July", 
     "August", "September", "October", "November", "December")
    for i in range(len(mon)):
        print(mon[i]+" "+str(day)+", "+str(year))

With this code I can get it to print all the dates from January to December in the correct format but I don't know what to change

for i in range(len(mon)):

to in order to select only the month entered in the argument.

EX: When entered into the console as date(6,17,2016) it should print June 17, 2016.

Mohd
  • 5,523
  • 7
  • 19
  • 30
Rob
  • 55
  • 4
  • 1
    `print(month[i-1]+" "+str(day)+", "+str(year))` or have a blank element in the beginning of tuple such as `mon = ("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")` and then use `print(month[i])`. – shahkalpesh Aug 27 '17 at 20:02
  • shahkalpesh's suggestion brings up IndexError: tuple index out of range. Coldspeed, I wish I didn't have to. – Rob Aug 27 '17 at 20:05
  • I updated my comment. What example causes the `IndexError`? – shahkalpesh Aug 27 '17 at 20:06
  • changing month[i] to month[i+1] but that makes sense with the +1. the problem here is that I don't know how to use an argument in the console to select only one month. – Rob Aug 27 '17 at 20:11

1 Answers1

3

You need to remove that for loop and access the mon tuple by index, for example:

def date(month, day, year):
    mon = ("January", "February", "March", "April", "May", "June", "July", 
     "August", "September", "October", "November", "December")
    print('{} {}, {}'.format(mon[month-1], day, year))

Since tuples indexing start from 0 you can access each month by using month-1 for example

1 - 1 = 0 for January since mon[0] is January.

Mohd
  • 5,523
  • 7
  • 19
  • 30