namespace issue?
Michael Powe
michael+gnus at trollope.org
Thu Jun 21 13:39:51 EDT 2001
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