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

Halfdan Holger Knudsen s011362 at student.dtu.dk
Wed Nov 19 10:33:33 EST 2003


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()





More information about the Python-list mailing list