Test@python关于列表及字典的应用_(2)

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

users = {
    'root':['redhat','2','None','18'],
}

info = '''

                            用户管理系统

      1. 注册新用户;
      2. 用户登陆
      3. 注销用户
      4. 显示用户信息
      5. 退出系统

请输入您的选择: '''
while 1:
    choice = raw_input(info).strip()

#*************************注册新用户************************
    if choice == '1':
        print '注册新用户'.center(40,'*')
        name = raw_input('*注册用户名: ')
        if name in users:
            print '该用户名已存在,请重新输入'
            continue
        else:
            users[name]=[]
            rpasswd = raw_input('*用户密码: ')
            users[name].append(rpasswd)
            while 1:
                gender = raw_input('*性别(0-女性 1-男性 2-其他):').strip()
                s = ['0','1','2']
                if gender not in s:
                    print '请输入正确选择'
                else:
                    users[name].append(gender)
                    break
            email = raw_input('*邮箱: ')
            if email == '':
                users[name].append('None')
            else:
                users[name].append(email)
            while 1:
                age = raw_input('*年龄: ')
                if age.isdigit():
                    users[name].append(age)
                    break
                elif age == '':
                    users[name].append('None')
                    break
                else:
                    print '输入格式有误,请输入数字或空'
                    continue
            print '新用户%s注册成功!!!'%(name)
            continue


#**************************用户登陆************************
    elif choice == '2':
        n = 0
        print '用户登录'.center(40,'*')
        iname = raw_input('*用户名: ')
        if iname not in users:
            print '用户名不存在!'
        else:
            while n < 3:
                ipasswd = raw_input('*密码: ')
                if not ipasswd == users[iname][0]:
                    print '密码错误,请重新输入'
                    n += 1
                    continue
                else:
                    print '用户 %s 登陆成功!!!'%(iname)
                    break
            else:
                print '超过三次,登陆失败!!!'


#**************************注销用户************************
    elif choice == '3':
        print '注销用户'.center(40,'*')
        iname = raw_input('*用户名: ')
        if iname not in users:
            print '用户名不存在!'
        else:
            ipasswd = raw_input('*密码: ')
            if not ipasswd == users[iname][0]:
                print '密码错误!'
            else:
                users.pop(iname)
                print '用户 %s 注销成功!!!' % (iname)

#*************************显示用户信息**********************
    elif choice == '4':
        print '显示用户信息'.center(40,'*')
        for i in users.keys():
            print '''用户:%s 性别:%s 邮箱:%s 年龄:%s'''%(i,users[i][1],users[i][2],users[i][3])

#*************************退出系统***********************
    elif choice == '5':
        print '您已退出'
        break
法二:
#!/usr/bin/env python
#coding:utf-8

users = {

    'root': {
        'name': 'root',
        'password': 'redhat',
        'gender': 2,
        'email': '',
        'age': 18
    },

}
info = '''

                            用户管理系统

      1. 注册新用户;
      2. 用户登陆
      3. 注销用户
      4. 显示用户信息
      5. 退出系统

请输入您的选择: '''
while 1:
    choice = raw_input(info).strip()

#*************************注册新用户************************
    if choice == '1':
        print '注册新用户'.center(40,'*')
        rname = raw_input('*注册用户名: ')
        if rname in users:
            print '该用户名已存在,请重新输入'
            continue
        else:
            rpasswd = raw_input('*用户密码: ')
            while 1:
                rgender = raw_input('*性别(0-女性 1-男性 2-其他): ').strip()
                s = ['0','1','2']
                if rgender not in s:
                    print '请输入正确选择'
                else:
                    break
            remail = raw_input('*邮箱: ')
            if not remail:
                remail = 'None'
            while 1:
                rage = raw_input('*年龄: ')
                if not rage:
                    rage = 'None'
                elif rage.isdigit():
                    break
                else:
                    print '输入格式有误,请输入数字或空'
                    continue
            users[rname]={
                'name':rname,
                'passwd':rpasswd,
                'gender':rgender,
                'age':rage,
                'email':remail,
            }
            print '新用户%s注册成功!!!'%(rname)
            continue

#**************************用户登陆************************
    elif choice == '2':
        n = 0
        print '用户登录'.center(40,'*')
        iname = raw_input('*用户名: ')
        if iname not in users:
            print '用户名不存在!'
        else:
            while n < 3:
                ipasswd = raw_input('*密码: ')
                if not ipasswd == users[iname]['passwd']:
                    print '密码错误,请重新输入'
                    n += 1
                    continue
                else:
                    print '用户 %s 登陆成功!!!'%(iname)
                    break
            else:
                print '超过三次,登陆失败!!!'


#**************************注销用户************************
    elif choice == '3':
        print '注销用户'.center(40,'*')
        iname = raw_input('*用户名: ')
        if iname not in users:
            print '用户名不存在!'
        else:
            ipasswd = raw_input('*密码: ')
            if not ipasswd == users[iname]['passwd']:
                print '密码错误!'
            else:
                users.pop(iname)
                print '用户 %s 注销成功!!!' % (iname)

#*************************显示用户信息**********************
    elif choice == '4':
        print '显示用户信息'.center(40,'*')
        for key,value in users.items():
            print '用户:%s'%(key),
            print '性别:%s'%(value['gender']),
            print '邮箱:%s'%(value['email']),
            print '年龄:%s'%(value['age'])

#*************************退出系统***********************
    elif choice == '5':
        print '您已退出'
        break
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值