[Tutor] date formatter
wesley chun
wescpy at gmail.com
Thu Aug 7 08:39:26 CEST 2008
> try:
> month = int(month)
> except ValueError:
> for eachKey in month_dict.keys():
> if month.lower() in eachKey:
> month = month_dict[eachKey]
> else:
> month = ""
> :
> I am having trouble dealing with the case where the user actually types in a month's name instead of the number:
> io at io-station-1 ./chap13 180> python date_format_v01.py
> :
> if month.lower() in eachKey:
> AttributeError: 'int' object has no attribute 'lower'
>
> Any suggestions?
chris,
good 1st attempt. all the problems are in the code snippet above. you
only have a few flaws in your program preventing it from working:
a. you "wipe out" the originally entered month (by continually
reassigning the month var)
b. you don't break out of the inner for-loop when u find a match and
end up wiping out the correct match
c. the cause of the exception: the entry for May is a string and not a
1-item tuple. you need a trailing comma "," after "may" to make it a
tuple. the reason why the error is triggered is because you set a
blank to month for every non-match. so when it gets to may, it makes
this comparison:
>>> "" in "may"
True
because of this, it sets...
month = month_dict["may"] # so month == 5
so in the next loop iteration, your're trying to do...
5.lower() in eachKey
which of course, fails. fix these problems, and you have a working solution!
best of luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Python Web Development with Django", Addison Wesley, (c) 2008
http://withdjango.com
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
More information about the Tutor
mailing list