How could i index the program to output e.g. displayDate(10, 2, 2009) gives "10 Feb 2009" as the output.

def displayDate(day, month, year):
    Months = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'August', 'Sept', 'October', 'December']

Recommended Answers

All 2 Replies

You pretty much had it there. Just use some string formatting.

def displayDate(day, month, year):
    m = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
    return "%d %s %d" % (day, m[month-1], year)

You got a good start and can can do it with a formatted string ...

def displayDate(day, month, year):
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Dec']
    print( "%s %s %s" % (day, months[month-1], year) )

# test it ...
displayDate(10, 2, 2010)  # 10 Feb 2010

Oops, Mathhax0r already answered this. I guess the trick is to index the month list correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.