namespace issue?

Michael Powe michael at trollope.org
Sat Jun 23 14:26:21 EDT 2001


>>>>> "Christopher" == Christopher A Craig <com-nospam at ccraig.org> writes:

    Christopher> Michael Powe <looie+gnus at aracnet.com> writes:
    >> Actually, it doesn't contain the dash.  The dash is only
    >> inserted if I press the enter key, leaving the PN string empty.
    >> In the case of an

    Christopher> I see that now.  I was really only paying attention
    Christopher> to the conditional...

    >> So now the question is, 'why'?  Am I misusing the string.find()
    >> function? Okay, string.find() returns -1 when the string is not
    >> found, which would seem to be what I expected, a false result.
    >> Maybe python doesn't treat -1 as false?  Well, I'll try being
    >> explicit and testing for -1 return.  I probably have just
    >> confused myself hopelessly.

    Christopher> As others have pointed out, -1 is not false in
    Christopher> Python.  I would highly recommend you (and anyone
    Christopher> else just learning the language) read section 2
    Christopher> (Built-in types) of the Library reference thoroughly.
    Christopher> There is a lot of very useful information on how
    Christopher> builtin types and functions work in there.

Thanks, I'll take a closer look.  I've only looked at it piecemeal,
trying to work things out.

    Christopher> More importantly though your code (which I quote
    Christopher> below for readability), does not initialize Ph if the
    Christopher> conditional is false.  Even if you fix the logic
    Christopher> error in the code, you are left with something that
    Christopher> possibly returns an uninitialized value, which is bad
    Christopher> practice.  I presume that you want the program to
    Christopher> return a value even when that condition is false, so
    Christopher> you should have an else clause.  If you want an error
    Christopher> condition to occur when the condition is false then
    Christopher> you should assert() the condition.

Right.  The end result looks like this:

def GetPhone():
    CC = raw_input("Country Code (ENTER for none): ")
    AC = raw_input("Area Code: ")
    PN = raw_input("Phone Number: ")
    if not AC : AC = '503'
    if not PN : PN = '000-0000'
    i = string.find(PN, '-')
    if len(PN) < 8 and i == -1 :
        Ph = '(' + AC + ') ' + PN[:3] + '-' + PN[3:]
    elif len(PN) >= 8 and i == -1 :
        Ph = CC + " " + PN[:4] + '-' + PN[4:]
    else :
        Ph = AC + PN
    return Ph

That seems to be working.  Obviously, I don't have any errorchecking
yet.  Functionality first.

Thanks for the help, I appreciate it.

mp

-- 
  Michael Powe                                 Portland, Oregon USA
'Unless we approve your idea, it will not be permitted, it will not be
allowed.'  -- Hilary Rosen, President, Recording Industry Association
of America



More information about the Python-list mailing list