zoukankan      html  css  js  c++  java
  • 三级菜单和登录认证程序

       这是刚开始写程序,三级菜单的程序基本是用字典实现,很low,以后学习了其他更好的东西,我会继续上传,然后争取在我水平高深之后,把这个简单的东西实现的狠高大上。

    一,三级菜单

    要求:

    • 可依次选择进入各子菜单
    • 可从任意一层往回退到上一层
    • 可从任意一层退出程序
    • 所需新知识点:列表、字典'''

    数据结构如下:

    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    

      

    1,一个简单的三级菜单的实现

      代码如下:

    menu = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'google': {}
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    '百度': {},
                },
            },
            '昌平': {
                '沙河': {
                    '老男孩': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            '闵行': {
                "人民广场": {
                    '炸鸡店': {}
                }
            },
            '闸北': {
                '火车战': {
                    '携程': {}
                }
            },
            '浦东': {},
        },
        '山东': {},
    }
    
    while True:
        for k in menu:
            print(k)
        choice1 = input("请输入地址(按b返回上一层,按q退出程序)
    ").strip()
        if not choice1:
            continue
        if choice1 in menu:
            while True:  # 进入第二层
                for k in menu[choice1]:
                    print(k)
                choice2 = input("请输入地址(按b返回上一层,按q退出程序)
    ").strip()
                if not choice2:
                    continue
                if choice2 in menu[choice1]:
                    while True:  # 进入第三层
                        for k in menu[choice1][choice2]:
                            print(k)
                        choice3 = input("请输入地址(按b返回上一层,按q退出程序)
    ").strip()
                        if not choice3:
                            continue
                        if choice3 in menu[choice1][choice2]:
                            print("go to ", menu[choice1][choice2][choice3])
                        elif choice3 == 'q':
                            exit("再见,欢迎再次使用")
                        elif choice3 == 'b':
                            break
                        else:
                            print("节点不存在")
                elif choice2 == 'b':
                    break
                elif choice2 == 'q':
                    exit("再见 ,欢迎再次使用")
                else:
                    print("节点不存在")
    
        elif choice1 == 'q':
            print("再见 ,欢迎再次使用")
            break
        elif choice1 == 'b':
            print("已经是最高层了,没有上一层")
            break
    

      此方法当然可以实现,就是代码比较冗余。

    2,一个高级的三级菜单的实现

      只用一个while循环,且整体代码量少于15行

    menu = {
        '北京':{
            '海淀':{
                '五道口':{
                    'soho':{},
                    '网易':{},
                    'google':{}
                },
                '中关村':{
                    '爱奇艺':{},
                    '汽车之家':{},
                    'youku':{},
                },
                '上地':{
                    '百度':{},
                },
            },
            '昌平':{
                '沙河':{
                    '老男孩':{},
                    '北航':{},
                },
                '天通苑':{},
                '回龙观':{},
            },
            '朝阳':{},
            '东城':{},
        },
        '上海':{
            '闵行':{
                "人民广场":{
                    '炸鸡店':{}
                }
            },
            '闸北':{
                '火车战':{
                    '携程':{}
                }
            },
            '浦东':{},
        },
        '山东':{},
    }
    current_layer = menu
    parent_list = []
    while True:
        for key in current_layer:
            print(key)
        choice = input("请输入地址,或者按b返回上一层,按q退出程序>>>:").strip()
        if len(choice) ==0:continue
        if choice in current_layer:
            parent_list.append(current_layer) #进入下一层之前保存当前层
            current_layer =current_layer[choice]   #将子层赋值给动态字典
        elif choice == 'b':
            if len(parent_list) !=0:
                current_layer =parent_list.pop()  #取出列表的最后一个值,并返回这个值
            else:print("已经是最后一层")
        elif choice =='q':break
        else:
            print("您所输入的地址不存在,请重输")
    

      此处实现的方式稍微高级一些,代码量也简洁了不少,而且方法更加巧妙,初学者要理解这里,则需要下很大的功夫。

    3,对于多级字典和列表的修改

      当我们将多级列表的内容写入一个空字典,然后在字典中修改内容,我们会发现列表的内容也相应的修改。

      因为我们使用的是同一个内存地址。

      如果说我们想只修改字典中的内容,而让列表中的内容不变,则我们需要copy列表。

      如下:

    menu_list = [
        {'id': 1, 'title': '菜单1'},
        {'id': 2, 'title': '菜单2'},
        {'id': 3, 'title': '菜单3'},
    ]
    
    menu_dict = {}
    
    for item in menu_list:
        menu_dict[item['id']] = item
        # print(item)  # {'id': 1, 'title': '菜单1'}
        # print(item['id'])  # 1
    print(menu_dict)
    
    # 当我们这样将 menu_list 写入 menu_dict中
    # 下面我们修改 mebu_dict中的值,menu_list中的值会发生改变???
    
    print(menu_list)
    menu_dict[2]['title'] = '修改后的菜单2'
    print(menu_list)
    '''
    [{'id': 1, 'title': '菜单1'}, {'id': 2, 'title': '菜单2'}, {'id': 3, 'title': '菜单3'}]
    [{'id': 1, 'title': '菜单1'}, {'id': 2, 'title': '修改后的菜单2'}, {'id': 3, 'title': '菜单3'}]
    
    '''
    

      字典的查找

    # 字典的查找
    menu_list = [
        {'id': 1, 'title': '菜单1'},
        {'id': 2, 'title': '菜单2'},
        {'id': 3, 'title': '菜单3'},
    ]
    
    # 请判断:id=2 的值在列表中是否存在?
    for item in menu_list:
        if item['id'] == 2:
            print("id=2 的值在列表中存在")
    
    
    # menu_dict = {}
    # for item in menu_list:
    #     menu_dict[item['id']] = item
    # print(menu_dict)
    
    menu_dict = {
                     1: {'id': 1, 'title': '菜单1'},
                     2: {'id': 2, 'title': '菜单2'},
                     3: {'id': 3, 'title': '菜单3'}
                }
    # 请判断:id=2 的值在字典中是否存在?
    
    if 2 in menu_dict:
        print("id=2 的值在字典中存在")
    

      但是当我们做如下实验,将一个变量追加到list中,然后修改列表中的变量,我们发现变量是不变的

    user = 'james'
    
    lis = []
    lis.append(user)
    print(lis)
    lis[0] = 'durant'
    print(lis)
    print(user)
    '''
    ['james']
    ['durant']
    james
    '''
    

      而当我们将字典的内容,放到列表中,也是同理,修改列表中的内容,字典的内容也会发生改变:

    dic = {
        'a': ['user1', 'user2'],
        'b': ['user3', 'user4']
    }
    lis = []
    
    for i in dic.values():
        lis.append(i)
    lis[0][0] = 'user11111111'
    print(dic)
    # 结果如下:
    # {'a': ['user11111111', 'user2'], 'b': ['user3', 'user4']}
    

      

      所以说,列表,字典,集合都是可变的数据类型。字符串,数字,元组均是不可变的数据类型。

       

    二,登录认证程序

    2.1 要求:

    基础需求:

    1. 让用户输入用户名密码
    2. 认证成功后显示欢迎信息
    3. 输错三次后退出程序

    升级需求:

    1. 可以支持多个用户登录 (提示,通过列表存多个账户信息)
    2. 用户3次认证失败后,退出程序
    3. 再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

    2.2 简单的认证程序

      这种代码可以完成任务,但是代码比较冗余,如果账号密码比较多,就炸了,但是还是实现了简单的认证程序。

    vip_username = ['james','wade','park','harden']
    vip_password = ['123456','234567','345678','456789']
    count = 0
    
    while True:
        username =input("请输入你的用户名:")
        if username not in vip_username:
            print("您所输入的用户不存在")
            continue
        else:
            with open("lock_name.txt", 'r+') as userlock:
                name_list = userlock.read()
                if username in name_list:
                    print(username + "该用户已经被锁定")
                    break
        password = input("请输入你的密码:")
    
        if username==vip_username[0] and password ==vip_password[0]:
            print("恭喜"+username+"登陆成功")
            break
        elif username==vip_username[1] and password ==vip_password[1]:
            print("恭喜" + username + "登陆成功")
            break
        elif username == vip_username[2] and password == vip_password[2]:
            print("恭喜" + username + "登陆成功")
            break
        elif username == vip_username[3] and password == vip_password[3]:
            print("恭喜" + username + "登陆成功")
            break
        else:
            count+=1
            print('用户密码错误,请你仔细核对')
            if count>=3:
                with open("lock_name.txt","a") as worryname:
                    worryname.write(username+","+"
    ")
                    print("输入密码错误次数过多,%s已经被锁定"%username)
                    break
    

      

    2.3 完善上面的代码

      上面代码有冗余的代码,我们这里修改一下,当然只针对上面代码修改:

    username = ['user1', 'user2', 'user3', 'user4']
    password = ['121', '122', '123', '124']
    count = 0
    
    # 为了方便,首先我们将username和password打包在一个字典里面
    userinfo = {}
    for i in range(len(username)):
        userinfo[username[i]] = password[i]
    # print(userinfo)  # {'user1': '121', 'user2': '122', 'user3': '123', 'user4': '124'}
    while True:
        inp = input("请输入您的VIP账号名:
    ")
        if inp not in username:
            print("您好,您输入的账号有误,请重新输入")
            continue
        else:
            with open('lock_name.txt', 'r+') as userlock:
                name_list = userlock.read()
                if inp in name_list:
                    print(inp + ' : 您已经被被锁定')
                    break
        pwd = input("请输入您的密码:
    ")
        if pwd == userinfo[inp]:
            print("登录成功")
            break
        else:
            print("密码错误,请重新输入")
            count += 1
            if count >= 3:
                with open('lock_name.txt', 'a') as worryname:
                    worryname.write(username + ',' + '
    ')
                    print("输入密码错误次数过多,%s已经被锁定" % username)
                    break
    

      上面代码其实是把Python基础学完后补充的,也算是对这篇博客进行完善吧。

  • 相关阅读:
    可以在Android上发定时短信
    Java的网络编程初识
    字节流与字符流的区别
    JAVA中的反射机制详解
    C语言C++语言中静态变量和静态函数
    Android网络编程
    C++中const用法总结
    Java中IO操作的基本规律
    面试智力题 (附答案)
    Java网络编程(总结)
  • 原文地址:https://www.cnblogs.com/wj-1314/p/7450089.html
Copyright © 2011-2022 走看看