利用字典管理用户的登录信息

来源:互联网 发布:国网网络大学怎么考试 编辑:程序博客网 时间:2024/05/18 07:20

利用字典管理用户的登录信息

我们用一个程序用于管理用户名和登录密码的模拟登录数据系统,脚本接受新用户的信息。
登录用户账号建立后,已经存在的用户可以用登录名字和密码重返系统,新用户则不能用别人的登录名建立用户账户。

# -*- coding: utf-8 -*-"""Created on Sun Aug 20 20:43:01 2017@author: zhang"""#!/usr/bin/env pythondb = {}def newuser():    prompt = 'login desired:'    while True:        name = raw_input(prompt)        if db.has_key(name):            prompt = 'name taken.try another:'            continue        else:            break    pwd = raw_input('passwd: ')    db[name] = pwddef olduser():    name = raw_input('login: ')    pwd = raw_input('passwd: ')    passwd = db.get(name)    if passwd == pwd:        print 'welcome back! old friend',name    else:        print 'login incorrect'def showmenu():    prompt = """    (N)ew User Login    (E)xisting User Login    (Q)uit system    Enter choice: """    done = False    while not done:        chosen = False        while not chosen:            try:                choice = raw_input(prompt).strip()[0].lower()            except (EOFError,KeyboardInterrupt):                choice = 'q'            print '\nYou picked: [%s]' % choice            if choice not in 'neq':                print 'invalid option,try again !'            else:                chosen = True        if choice == 'q':            done = True        if choice == 'n':            newuser()        if choice == 'e':            olduser()if __name__=='__main__':    showmenu()

运行的结果如下:

----------    (N)ew User Login    (E)xisting User Login    (Q)uit system    Enter choice: nYou picked: [n]login desired:abcpasswd: 123    (N)ew User Login    (E)xisting User Login    (Q)uit system    Enter choice: nYou picked: [n]login desired:cdepasswd: 234    (N)ew User Login    (E)xisting User Login    (Q)uit system    Enter choice: eYou picked: [e]login: abcpasswd: 123welcome back! old friend abc    (N)ew User Login    (E)xisting User Login    (Q)uit system    Enter choice: qYou picked: [q]
原创粉丝点击