Python第四天课后作业

设计一个用户管理系统

#!/usr/bin/env python
# coding:utf-8

user = {}  # 定义一个字典,用于存放普通用户信息,初始值为空

def Administrator_Menu():
    while 1:
        Menu = """

            **** Administrator Function List ****
            *                                   *
            *           1.New Users             *
            *           2.Print All Users       *
            *           3.exit                  *
            *                                   *
            *************************************
                 (Admin)input your choice:"""
        choice = input(Menu)
        if choice == 1:
            new_username = raw_input("input new username:")
            new_password = raw_input("input new password:")
            if user.has_key(new_username):  # 经检验,该用户已存在于字典中,无法添加
                print "  %s already exists,please input again." % (username)
            else:
                user.setdefault(new_username, new_password)  # 经检验,原字典中不存在该用户,成功添加

        elif choice == 2:
            if len(user) == 0:  # 字典为空,即不存在用户
                print "list is empty."
            else:
                for i in user.keys():
                    print "username:%10s  password:%10s" % (i, user[i])  # 打印字典

        elif choice == 3:
            return 1         #返回主菜单
        else:
            print "input error."


def Ordinary_User_Menu(username):
    while 1:
        Menu = """

            **** Ordinary User Function List ****
            *                                   *
            *       1.Personal information      *
            *       2.log off                   *
            *       3.exit                      *
            *                                   *
            *************************************
                    input your choice:"""
        choice = input(Menu)
        if choice == 1:
            print username, user[username]  # 显示个人信息
        elif choice == 2:
            del (user[username])  # 注销用户
            return 0
        elif choice == 3:
            return 1      #返回主菜单



while 1:
    Menu = """
    **** Welcome to CRM ****
    *                      *
    *       1.login        *
    *       2.exit         *
    *                      *
    ************************
      input your choice:"""
    choice = input(Menu)
    if choice == 1:
        username = raw_input("please input username:")
        password = raw_input("please input password:")
        if username == 'westos' and password == 'redhat':  # 判断其为管理员账户,若登陆成功,则打印管理员菜单
            Administrator_Menu()
        elif user.has_key(username):            #判断所输入用户名是否存在于字典中
            if password==user[username]:        #进一步匹配密码
                Ordinary_User_Menu(username)    #打印普通用户菜单
            else:
                print "sorry,your password is wrong."
        else:
            print "sorry,your username is wrong."

    elif choice == 2:
        exit(0)
    else:
        print "input error."

主菜单界面,选择登陆,并输入管理员账户和密码:
这里写图片描述

管理员菜单界面,新添两名用户
这里写图片描述

为了验证是否添加成功,打印当前字典中的所有用户
这里写图片描述

退出管理员菜单,返回主菜单界面,验证刚才注册的两名用户是否能够成功登陆
这里写图片描述

成功登陆用户Cecilia,打印个人信息
这里写图片描述

注销用户Cecilia,自动返回主菜单
这里写图片描述

猜你喜欢

转载自blog.csdn.net/siyuexiangxian/article/details/77689089
今日推荐