Dennis Lee Bieber
1/21/2008 4:31:00 AM
On Mon, 21 Jan 2008 12:02:50 +0800, "J. Peng" <jpeng@block.duxieweb.com>
declaimed the following in comp.lang.python:
> Please see the code below,what's the scope for object "name"?
Please repost using a client that doesn't left justify all lines --
your post has lost all indentation, so it is nearly impossible to
determine what constitutes the while block.
> I thought it should be located in the while block, but it seems not
> really,it can be accessed out of while (the db[name] statement).Thanks
> in advance.
>
>
Guessing at the indentation:
> db = {}
> def newuser():
> prompt = 'login desired: '
> while 1:
Any recent Python should take
while True:
> name = raw_input(prompt)
> if db.has_key(name):
> prompt = 'name taken, try another: '
> continue
continue is not needed
> else:
> break
The whole loop can turn into
while True:
name = raw_input(prompt)
if name not in db: break
prompt = "'%s' is in use, choose another: " % name
> pwd = raw_input('passwd: ')
> db[name] = pwd
The scope of "name" is the entire function; lacking a "global name"
statement, AND being on the left side of an assignment, it is a function
local name.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/