hey i just started python.. i know this is a useless stupid program but i still cant get it right..:(
here i want the user to enter date in dd/mm/yyyy fashion and return the date in words. eg. 12/12/1912= 12th December,1912
but it doesnt work.. it only works for 29 feb, but all other dates the output is invalid date..plz help!!

'''date'''
datein=raw_input("Plz enter date in dd/mm/yyyy format:")
day,month,year=datein.split('/')
mnth=[None,'January','February','March','April','May','June','July','August','September','October','November','December']
chk={"2":28}
for i in [1,3,5,7,8,10,12]:
	chk[i]=31
for i in [4,6,9,11]:
	chk[i]=30
idx=[None,'st','nd','rd']#for adding the 'th' and stuff in the date
idx+=17*['th']
idx+=['st','nd','rd']
idx+=7*['th'];
idx.append('st')
if month==2 and day==29:
	print "29th February,"+year
	print
elif month <=12 and (int(day)<=chk[str(month)]):
	print day + idx[int(day)] + mnth[int(month)]+", "+ year
	print
else:
	print "Invalid date entered"

The date is invalid because day month and year are of type string and you are trying to compare them to an integer

Here is the corrected code

'''date'''
datein=raw_input("Plz enter date in dd/mm/yyyy format:")
day,month,year=datein.split('/')
mnth=[None,'January','February','March','April','May','June','July','August','September','October','November','December']
chk={"2":28}
for i in [1,3,5,7,8,10,12]:
        chk[i]=31
for i in [4,6,9,11]:
        chk[i]=30
idx=[None,'st','nd','rd']#for adding the 'th' and stuff in the date
idx+=17*['th']
idx+=['st','nd','rd']
idx+=7*['th'];
idx.append('st')
if int(month) <=12 and (int(day)<=chk[int(month)]):
        print day + idx[int(day)] + " " + mnth[int(month)]+", "+ year
        print
else:
        print "Invalid date entered"

1. Your jumble of code is going to give us pythoneers a heart attack!!! (Of course, you are forgiven as you are a newbie!)

2. Please use click on the 'Help with Code Tags' to see how you must post your code.

3. Check out the code below. I haven't used any functions as you did not either in your code.

import string

# vars
ordinal = ''

date = raw_input('Enter date : (Format dd/mm/yyyy) : - ')

day, month, year = date.split('/')

if eval(day) in (1, 21, 31):
    ordinal = 'st'
elif eval(day) in (2, 22):
    ordinal = 'nd'
elif eval(day) in (3, 23):
    ordinal = 'rd'
else:
    ordinal = 'th'

monthString = ['January','February','March','April','May','June','July','August','September','October','November','December']

print day + ordinal + ' ' + monthString[eval(month)-1] + ', ' + year

You had written the following piece of code in your program

.
chk={"2":28}

What does is do, or what is it that you wanted it to do?

hehe.. sry.. i will get better i promise you :)
thanks alot
also, is there a way to find out what a certain code does? for eg.. i had no idea about the eval function used..
i tried dir(eval) but it gave a list of a bunch of names of functions that i couldnt quite understand..

You had written the following piece of code in your program

.
chk={"2":28}

What does is do, or what is it that you wanted it to do?

its used to check the validity of the input.. like you cannot enter 31/4/2009 as april does not have 31 days

You need to have the python chm manual so that you could search for the functions you need. You could type the function name in the search box that it provides.

Better still, if you could get a hard copy of a book called 'Python in a Nutshell'. It will give you examples.

You could use any Python interactive interface (the one that starts with the prompt >>>) for example IDLE. Infact all editors provide the interactive interface so that we can test snippets of code to see if it works

A good part of coding in Python is to know what its modules are doing. The module datetime makes your life a lot simpler, it also flags down impossible dates:

# convert one date format to another
# check the Python manual under strftime()
# to get the % format directives

import datetime as dt

def convert_date(date_str):
    """convert date_str eg. 11/21/1987 to 21 November,1987"""
    try:
        fdate = dt.datetime.strptime(date_str, "%m/%d/%Y")
        return fdate.strftime("%d %B,%Y")
    except ValueError:
        return False
        
    
date_str = "11/21/1987"
print(convert_date(date_str))  # 21 November,1987

# test impossible dates
print(convert_date("11/31/1987"))  # False
print(convert_date("2/29/1987"))   # False

In its simplest form:

import datetime as dt

ds = "11/21/1987"
print(dt.datetime.strptime(ds, "%m/%d/%Y").strftime("%d %B,%Y"))

"""
my result -->
21 November,1987
"""
This article has been dead for over six months. Start a new discussion instead.