# 一、【用户登陆程序】
# 基础需求:
# 让用户输入用户名密码
# 认证成功后显示欢迎信息
# 输错三次后退出程序
count = 0 name = "cheng" password = "123456" while count <3: count += 1 your_name = input ("请输入你的名字:") your_pwd = input ("请输入你的密码:") if your_name == name and your_pwd == password: print("登录成功,欢迎进入..") break else: print("名字或密码错误,你还有%s次机会" % (3-count))
# 升级需求:
# 可以支持多个用户登录 (提示,通过列表存多个账户信息)
# 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
lock = "lock" account = "account" flag = 1 count = 0 lock_user = [] file1 = open(lock, "r") lock_file = file1.readline() file1.close() for i in lock_file: i = i.strip(" ") lock_user.append(i) file2 = open(account, 'r') account_file = file2.readlines() file2.close() while True: your_name = input("请输入你的名字:") your_pwd = input("请输入你的密码:") if your_name in lock_file: print("你的用户名已经给锁定,请联系管理员") break else: count += 1 if count > 2: print("你的用户名已经给锁定,请联系管理员") with open(lock, "a") as file: file.write(your_name + " ") break else: for i in account_file: name, password = i.strip(" ").split() if your_name == name and your_pwd == password: print("登录成功,欢迎进入") flag = True else: continue if flag is True: break
# 二、【三级菜单】
# 数据结构:
#
# 需求:
# 可依次选择进入各子菜单
# 可从任意一层往回退到上一层
# 可从任意一层退出程序
# 所需新知识点:列表、字典
menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': { '百度': {}, }, }, '昌平': { '沙河': { '老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': { '闵行': { "人民广场": { '炸鸡店': {} } }, '闸北': { '火车站': { '携程': {} } }, '浦东': {}, }, '山东': {}, } layer = [menu] while layer: for i in layer[-1]:print(i) choice = input(">>:).strip() if choice in layer[-1].key() and layer[-1][choice]: layer.append(layer[-1][choice]) elif choice == "b":layers.pop() elif choice.upper() =="Q": break