[Python学习日记-29] 开发基础练习2——三级菜单与用户登录

[Python学习日记-29] 开发基础练习2——三级菜单与用户登录

简介

三级菜单

用户登录

简介

        该练习使用了列表、字典、字符串等之前学到的数据类型,用于巩固实践之前学习的内容。

三级菜单

一、题目

数据结构:

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

需求:

  • 可依次选择进入各子菜单
  • 可从任意一层往回退到上一层
  • 可从任意一层退出程序

所需知识点:列表、字典

二、答案——普通版

v1.0.1:

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

while True:
    for k in menu:
        print(k)
    choice = input(">:").strip()
    if not choice:continue
    if choice in menu:
        while True: # 进入第二层
            for k in menu[choice]:
                print(k)
            choice2 = input(">>:").strip()
            if not choice2:continue
            if choice2 in menu[choice]:
                while True: # 进入第三层
                    for k in menu[choice][choice2]:
                        print(k)
                    choice3 = input(">>>:").strip()
                    if not choice3:continue
                    if choice3 in menu[choice][choice2]:
                        print("go to ",menu[choice][choice2][choice3])
                    elif choice3.lower() == "b":
                        break
                    elif choice3.lower() == "q":
                        quit()
                    else:
                        print("节点不存在")
            elif choice2.lower() == "b":
                break
            elif choice2.lower() == "q":
                quit()
            else:
                print("节点不存在")
    elif choice.lower() == "b":
        print("无路可退")
    elif choice.lower() == "q":
        quit()
    else:
        print("节点不存在")

代码输出如下: 

v1.0.2:

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

# 保存进入的每一层记录
current_layer = menu
layers = []
while True:
    for k in current_layer:# menu[北京]
        print(k)
    choice = input(">>:").strip()
    if not choice:continue
    if choice in current_layer:# menu[北京]
        layers.append(current_layer)# 进入下一层,保存当前层
        current_layer = current_layer[choice]# menu[北京][昌平]
    elif choice == 'b':
        if len(layers) != 0:
            current_layer = layers.pop()
        else:
            print("已经是顶层!")

代码输出如下: 

 

三、答案——进阶版

# -*- coding:utf-8 -*-
layers = [{'北京': {'海淀': {'五道口': {'soho': {}, '网易': {}, 'google': {}}, '中关村': {'爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': {'百度': {}, }, }, '昌平': {'沙河': {'新东方': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': {'闵行': {'人民广场': {'炸鸡店': {}}}, '闸北': {'火车站': {'携程': {}}}, '浦东': {}, }, '山东': {}, }]
while True:
    [print(k) for k in layers[-1]] # menu[北京]
    choice = input(">>:").strip()
    if choice in layers[-1]:layers.append(layers[-1][choice]) # 进入下一层,保存当前层
    elif choice.lower() == 'b' and len(layers) > 1:del layers[-1]
    elif choice.lower() == 'q':quit("有缘再见")
    else:print("输入异常")

代码输出如下: 

用户登录

一、题目

基础需求:

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

升级需求:

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

二、答案

1、基础需求版本

# -*- coding:utf-8 -*-

_user = "alex"
_passwd = "abc123"

count = 0

while count < 3:
    username = input("Username:").strip()
    password = input("Password:").strip()

    if username == _user and password == _passwd:
        print("welcome ...")
        break
    else:
        print("Wrong username or password.")
    count += 1
else:
    print("Too many attempts.")

# 连续输错三次,并不是输错三次
accounts = {
    'alex': ['abc123', 0],
    'jack': ['1413', 1],
    'rain': ['ddsss', 0]
}

代码输出如下: 

 

2、升级需求版本

user_data:(用于存储用户信息)

{'jove': ['abc123', 0], 'jack': ['1413', 0], 'rain': ['ddsss', 0]}        # 用户名:[密码,锁定状态]

# -*- coding:utf-8 -*-

f = open('user_data','r')
accounts = eval(f.read())

count = 0
last_username = None
is_same_user = True

while count < 3:
    username = input("Username:").strip()
    password = input("Password:").strip()

    if last_username is None:  # 这肯定是第一次
        last_username = username

    if last_username != username:  # 代表这一次输入的跟上一次用户不一致
        is_same_user = False  # 小旗子降下来

    if username in accounts:
        if accounts[username][1] == 0:  # 判断用户是否锁定
            if password == accounts[username][0]:
                print("welcome ...")
                break
            else:
                print("Wrong username or password.")
        else:
            print("Account was lock,pleace call Administrator.")
    else:
        print("Account not live.")
    count += 1
else:
    print("Too many attempts.")
    if is_same_user == True:  # 3次输入的一致,锁定用户
        accounts[username][1] = 1
        f = open('user_data','w')
        f.write(str(accounts))
        f.close()

代码输出如下: 

账号锁定后的文件状态:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JoveZou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值