python核心编程7.5答案ver1

来源:互联网 发布:java 图片上传 跨域 编辑:程序博客网 时间:2024/06/06 17:29

学习python中由于写到一半发现纯过程的话有点乱,学完对象之后再更新功能,没有实现时间戳,以及密码验证,非法字符验证也未添加....以后会持续更新

#!/usr/bin/env python

#coidng=utf-8
import os
import sys
#初始化,后便测试manager
db = {'x':'y', 'a':'b', 'a':'d'}
def clear():
    os.system("clear")
#清屏
def show():
    clear()
    for item in db.items():
        print item
#manager 用于管理显示所有账户信息
def delete():
    name = raw_input('please input the name you want to del:')
    if not db.has_key(name):
        print "the name you input not exists"
    else:
        del db[name]
#manager用于删除账户
def manager():
    name = raw_input('login:')
    if name == 'manager':
        pwd = raw_input('passwd')
        if pwd == '000000':
            print "welcome manager"
            while True:
                print '''\
(S)how all information of usrs,
(d)el a usr,
(q)uit.'''
                try:
                    choice = raw_input("please input your choice:").strip()[0].lower()
                except (EOFError, KeyboardInterrupt):
                    choice  = 'q'
                if choice not in 'sdq':
                    print "invalid input, please try again"
                else:
                    if choice == 's': show()
                    if choice == 'd': delete()
                    if choice == 'q': break
        else:
            print 'the password is not right'
    else:
        print 'the manager name is not right'
def usr():
    name = raw_input('login:').lower()
    if db.has_key(name):
        pwd = raw_input('passwd:')
        passwd = db.get(name)
        if passwd == pwd:
            print "welcome back", name
        else:
            print "passwd was not right(if you are a new usr try a new name)"
    else:
        s = raw_input("if you are a new user? print yes, else: print no:").strip().lower()
        if s == 'yes':
            pwd = raw_input('passwd:')
            db[name] = pwd
            print "welcome!!!", name
def showmenu():
    prompt = '''\
(M)anager
(u)sr login
(Q)uit
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 have [picked]: [%s] " % choice
            if choice not in 'muq':
                print 'invalid option, tru again'
            else:
                chosen = True
            if choice == 'm':manager()
            if choice == 'q':done = True
            if choice == 'u':usr()
if __name__ == '__main__':
    showmenu()

0 0