python核心编程 练习题7.5

7–5. userpw2.py. 下面的问题和例题 7.1 中管理名字-密码的键值对数据的程序有关。

(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用 time 模块),并与用户密码一起保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示,在户名成功登录后,应更新相应用户的上次登录时间戳。 如果本次登录与上次登录在时间上相差不超过 4 个小时,则通知该用户: “You already logged in at: <last_ login_timestamp>.”

(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名字和他们的密码的清单。

(c) 口令目前没有加密。 请添加一段对口令加密的代码(请参考 crypt, rotor, 或其它加密模块

(d) 为程序添加图形界面,例如,用 Tkinter 写。

(e) 要求用户名不区分大小写。

(f) 加强对用户名的限制,不允许符号和空白符。

(g)合并“新用户”和“老用户”两个选项。 如果一个新用户试图用一个不存在的用户名登录,询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。


TOT还没学会加密模块和图形界面使用。只能简单完成(a)(b)(e)(f)(g)的功能。代码如下

db = {}
import time
def check(name):
    '''
检查用户名是否合法,如果用户名合法,返回小写的用户名保存
'''
    lenname=len(name)
    ifok=True
    name1=''
    for i in name:
        a=ord(i)
        if 47<a<58 or 96<a<123:
            name1=name1+chr(a)
        elif 64<a<91:
            name1=name1+chr(a+32)
        else:
            ifok=False
            break
    if ifok:
        return name1
    else:
        return False

def newuser(name):
    '''
确认是否为新用户,如果为新用户则添加账户
'''
    prompt = """
New User?
(Y)es
(N)o
Enter choice: """
    done = False

    while not done:
        
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'n'
            print ('\nYou picked: [%s]' % choice)
            if choice not in 'yn':
                print ('invalid option, try again')
            else:
                if choice == 'y':
                    pwd = input('passwd: ')
                    time1=time.time()
                    db[name] = [pwd,time1]
                done = True
                break

def olduser(name):
    '''
老用户
'''
    pwd = input('passwd: ')
    info = db.get(name)
    if info[0] == pwd:
        print ('welcome back', name )
        if time.time()-info[1]>14400:
               print ("You last logged time is:",time.ctime(info[1]))
    else:
        print ('login incorrect')
def delete(name):
    '''
删除账号
'''
    if db:
        if name not in db:
            print('name not found, try another: ')
        else:
            del db[name]
    else:
        print('no account exist')
def show():
    '''
显示账号
'''
    for i in db:
        print('account:',i," password:",db[i][0])
def user():
    '''
账号输入菜单
'''
    prompt = 'login: '
    while True:
        name = input(prompt)
        if check(name)==False:
            prompt = 'name illegal, try another: '
            continue
        else:
            name=check(name)
            break
    if name not in db:
        newuser(name)
    else:
        olduser(name)
        
def manage():
    '''
管理菜单
'''
    prompt = """
(1)delete account
(2)showlist
(Q)uit
Enter choice: """
    
    done = False
    while not done:
        
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print ('\nYou picked: [%s]' % choice)
            if choice not in '12q':
                print ('invalid option, try again')
            else:
                if choice == 'q':
                    done = True
                    break
                if choice == '1':
                    name=input('input the account you want to delete:')
                    delete(name)
                if choice == '2':
                    show()
    

def showmenu():
    '''
主菜单
'''
    prompt = """
(U)ser Login
(M)anage
(Q)uit
Enter choice: """

    done = False
    while not done:
        
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print ('\nYou picked: [%s]' % choice)
            if choice not in 'uqm':
                print ('invalid option, try again')
            else:
                if choice == 'u':
                    user()
                elif choice == 'm':
                    manage()
                else:
                    print ('quit!')
                    return

if __name__ == '__main__':
    showmenu()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值