It's a bug!....but is it me or the script? (Global var assign)

  • Thread starter Halfdan Holger Knudsen
  • Start date
H

Halfdan Holger Knudsen

Hey here's a relatively simple question...the code is for a mock-up passwd, usrname dict storagesystem. Why doesn't the variable enc behave globally (the admin function turns up a "local var referenced before assignment" error, when inputting 's')? In all likelyhood I've just managed to stare myself blind, so to anyone w/ a spare pair of eyes - the help would be appreciated. It's run under Python 2.3.0 in Win2k.

T/x
H

PS: I know it's long and I apologize, but basically it's just the first 8 lines that should matter.

-----------------------------------

#!/usr/bin/env python

import time
import rotor

passenc = rotor.newrotor('lockkey', 8)
db = {}
enc = 0

'Solution for exercise 7 a, b and c. Unresolved: Why is enc treated as global in newuser() but local in admin()?'

def newuser():
prompt = 'login desired: '
while 1:
name = raw_input(prompt)
if len(name) < 3:
prompt = 'name too short, try another: '
continue
elif db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')
# If admin has turned in encryption encrypt new pass. Unresolved issue regarding switching the feature on and off (conditional decrypt)
if enc:
pwd = passenc.encrypt(pwd)
time1 = time.time()
#creates the user w/ a name, pass and a timestamp for last login
db[name] = [pwd, time1, enc]
print name, db[name][0], db[name][1], db[name][2]

def olduser():
name = raw_input('login: ')
pwd = raw_input('passwd: ')
passwd = db.get(name)[0]
#Possible solution for conditional decrypt
if db.get(name)[2]:
passwd = passenc.decrypt(passwd)
#checks if pass is good and updates timestamp (saves a copy for comparison)
if passwd == pwd:
chktime = db[name][1]
time1 = time.time()
db[name][1] = time1
#checks if last login is less than four hours ago
if time1-chktime < 14400:
print 'You already logged in at '+time.strftime('%H:%M',time.localtime(chktime))
pass
else:
print 'login incorrect'
return

print 'welcome back', name

def admin():
prompt = """
(V)iew list of users
(D)elete user account
(S)et password encryption
(Q)uit

Enter choice: """

while 1:
adchoice = raw_input(prompt)
if adchoice not in 'vdqs':
prompt = 'invalid choice, try again: '
continue
elif adchoice == 'v':
for eachkey in db.keys():
print 'user', eachkey, 'has password', db[eachkey][0]
prompt = 'Enter new choice: '
continue
elif adchoice == 'd':
delusr = raw_input('User to remove: ')
if db.has_key(delusr):
del db[delusr]
prompt = 'User deleted. Enter new choice: '
else:
prompt = 'No such user. Enter new choice: '
continue
elif adchoice == 's':
if enc==1:
enc = 0
print 'Encryption off'
continue
else:
enc = 1
print 'Encryption on'
continue
elif adchoice == 'q':
break


def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(A)dministration
(Q)uit

Enter choice: """

done = 0
while not done:
chosen = 0
while not chosen:
try:
choice = raw_input(prompt)[0]
except (EOFError, KeyboardInterrupt):
choice = 'q'
print '\nYou picked: [%s]' % choice

if choice not in 'aneq':
print 'invalid menu option, try again'
else:
chosen = 1

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()
if choice == 'a': admin()

if __name__ == '__main__':
showmenu()
 
P

Peter Hansen

Halfdan said:
Why doesn't the variable enc behave globally (the admin function turns
up a "local var referenced before assignment" error, when inputting 's')?

To use a variable globally, *if you're going to rebind it* (as in, assign to it)
you need to put a "global" statement at the top of each function which uses it.

If all you ever do is read the value, you don't need this, but you are trying
to rebind the name "enc" in some of those functions, so you must have a
"global enc" statement before you do that.

-Peter
 
J

JCM

You must declare a module-level variable global in a function if you
wish to assign to it:

x = 0

def f():
global x
x = 1
 
P

Peter Otten

Halfdan said:
Hey here's a relatively simple question...the code is for a mock-up
passwd, usrname dict storagesystem. Why doesn't the variable enc behave
globally (the admin function turns up a "local var referenced before
assignment" error, when inputting 's')? In all likelyhood I've just
managed to stare myself blind, so to anyone w/ a spare pair of eyes - the
help would be appreciated. It's run under Python 2.3.0 in Win2k.

T/x
H

PS: I know it's long and I apologize, but basically it's just the first 8
lines that should matter.

When you try to make it shorter instead of staring yourself blind the chance
of finding the bug yourself increases rapidly:
.... if e == 0:
.... e = 1
....Traceback (most recent call last):
File "<stdin>", line 1, in ?
.... global e
.... if e == 0:
.... e = 1
....
When you are rebinding a name (enc) anywhere inside a function it refers to
a local even before the actual rebinding is done, unless you explicitly
state you want it to refer to a global, i. e. say

global enc

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top