namespace issue?

Emile van Sebille emile at fenx.com
Thu Jun 21 14:24:15 EDT 2001


The problem is likely that your 'if len(PN)...' test is false, leaving Ph
unassigned when the return executes.

HTH,

--

Emile van Sebille
emile at fenx.com

---------
"Michael Powe" <michael+gnus at trollope.org> wrote in message
news:uwv65ivfc.fsf at trollope.org...
> Hello,
>
> This looks like a namespace issue described in <learning python> but
> it doesn't make sense to me and I don't know how to fix it.
>
> ex 1:
>
> >>> def GetNum():
> ... AC = raw_input("Area Code: ")
> ... PN = raw_input("Phone Number: ")
> ... if not AC : AC = '503'
> ... if not PN : PN = '000-0000'
> ... Ph = AC + PN[:3] + '-' + PN[3:]
> ... return Ph
> ...
> >>> ok = GetNum()
> Area Code: 503
> Phone Number: 2243187
> >>> ok
> '503224-3187'
>
> ex 2:
>
> >>> def GetNum2():
> ... AC = raw_input("Area Code: ")
> ... PN = raw_input("Phone Number: ")
> ... if not PN : PN = '000-0000'
> ... if not AC : AC = '503'
> ... if len(PN) < 8 and not string.find(PN,'-'):
> ... Ph = PN[:3] + '-' + PN[3:]
> ... return Ph
> ...
> >>> ok = GetNum2()
> Area Code: 434
> Phone Number: 2243187
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 8, in GetNum2
> UnboundLocalError: local variable 'Ph' referenced before assignment
>
> The issue is, I don't understand why the problem appears with variable
> 'Ph' in the second instance but not in the first.  It seems obvious
> that it comes about because of the conditional.
>
> Also, on a side note, what is the default data type for input gathered
> from raw_input()?  Is it a string?
>
> Thanks for any help.
>
> mp
>
>





More information about the Python-list mailing list