16.python模拟登录程序和三级菜单
模拟登录程序需求:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
#!/usr/bin/env python #-*- coding: utf-8 -*- i=0 while i<3: username = input("请输入您的用户名:") lock_file = open('account_lock','r') lock_list = lock_file.readlines() for lock_line in lock_list: lock_line =lock_line.strip('\n') if username == lock_line: lock_file.close() exit("您的账户已被锁定") user_account = open('user_account','r') user_list = user_account.readlines() for user in user_list: (user_infile,password_infile) = user.strip('\n').split() if username == user_infile: j=0 while j<3: password=input("请输入您的密码:") if password == password_infile: user_account.close() exit("登录成功,欢迎您") else: print("您输入的密码错误,您还有%s次机会输入"%(2-j)) j += 1 else: lock_file = open('account_lock','a') lock_file.write(username +'\n') exit("密码输入次数超限,您的用户已被锁定") else: print("您输入的用户名不存在,您还有%s次机会"%(2-i)) i += 1 lock_file.close() user_account.close()
三级菜单
作业需求:
1. 运行程序输出第一级菜单
2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单
3. 菜单数据保存在文件中
4. 让用户选择是否要退出
5. 有返回上一级菜单的功能
代码如下:
# Author:pengp # !/usr/bin/env python # -*- coding: utf-8 -*- menu = { "上海": { "浦东": ["张江", "唐镇"], "嘉定": ["娄塘", "封浜"], "宝山": ["罗店", "月浦"] }, "北京": { "海淀": ["上地", "中关村"], "朝阳": ["朝阳一镇", "朝阳二镇"] }, "江苏": { "南京": ["浦口", "六合"], "苏州": ["昆山", "太仓"], "南通": ["如东", "启东"] }, "湖北": { "武汉": ["汉口", "武昌"], "黄冈": ["麻城", "罗田"], "孝感": ["应城", "大悟"] } } current_level = menu last_level = [] while True: for key in current_level: print(key) choice = input(">>>:") if len(choice) == 0:continue if choice == "b": if not last_level : break current_level = last_level[-1] last_level.pop() elif choice == 'q': exit("退出成功") if choice not in current_level:continue last_level.append(current_level) current_level = current_level[choice]