[Tutor] correcting an Active State Recipe for conversion to ordinal

Serdar Tumgoren zstumgoren at gmail.com
Thu Feb 4 19:21:38 CET 2010

> Perhaps the code on activestate is not a correct copy of what you are
> running? The conditional at line 23 extends all the way to line 35 -
> the end of the function - so if value % 100/10 == 1 no more code is
> executed and None is returned.

Here is the code that I'm running locally (hopefully the formatting
doesn't get screwed up):

def ordinal(value):
    Converts an integer to it's ordinal as a string.
    For example 1 to "1st", 2 to "2nd", 3 to "3rd", etc.
        value = int(value)
    except ValueError:
        return value

    if value % 100/10 <> 1:
        if value % 10 == 1:
            ord = u"%d%s" % (value, "st")
            return ord
        elif value % 10 == 2:
            ord = u"%d%s" % (value, "nd")
            return ord
        elif value % 10 == 3:
            ord = u"%d%s" % (value, "rd")
            return ord
            ord = u"%d%s" % (value, "th")
            return ord
        ord = u"%d%s" % (value, "th")
        return  ord

I had eyeballed the ActiveCode and compared to my local code, and it
looked identical...but perhaps it was too cursory a check.

I'll download a copy from ActiveState and compare to my local code. If
that doesn't turn up a solution, I'll also do some investigating on
the modulus operator any possible differences between its
implementation/operation between versions of Python. If it helps, I'm
using Python 2.6.2...

Thank you all for the tips so far!


More information about the Tutor mailing list