作业二:编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
FlowChart:
代码:
#!usr/bin/env python # -*- coding:utf-8 -*- # Author: Wayne Zhong user = 'Wayne' pwd = '123465' f = open('status.txt') if f.read() == 'Y': for i in range(3): username = input('Input username:') password = input('Input password:') if username == user and password == pwd: print('Welcome login.') break else: print('User invalid.') else: f = open('status.txt','w') f.write('N') print('3 times wrong input, account is blocked.') else: print('Account is blocked.')
作业三:多级菜单
- 三级菜单
- 可依次选择进入各子菜单
- 所需新知识点:列表、字典
Flowchart:
代码:
#!usr/bin/env python # -*- coding:utf-8 -*- # Author: Wayne Zhong city_dict = { '1.北京':{ '(1)朝阳' : ['1.白静', '2.邹清', '3.群众'], '(2)海淀' : ['1.张佳', '2.王迪', '3.学霸'], '(3)大兴' : ['1.尹伟', '2.苏慧', '3.金良'] }, '2.上海':{ '(1)浦东' : ['1.Andy', '2.黄渊', '3.蒋一'], '(2)徐汇' : ['1.马伟', '2.高成', '3.吴成'], '(3)宝山' : ['1.刘松', '2.王丹', '3.祥子'] }, '3.深圳':{ '(1)罗湖' : ['1.陈建', '2.范志', '3.刘洋'], '(2)南山' : ['1.邓明', '2.黄灿', '3.陆坤'], '(3)宝安' : ['1.范波', '2.李松', '3.王瑶'] }, '4.西安':{ '(1)长安' : ['1.陈琳', '2.Meth', '3.何潇'], '(2)雁塔' : ['1.段佳', '2.邱力', '3.杨龙'], '(3)未央' : ['1.光圈', '2.王盼', '3.孙欢'] } } def district(): while(1): d = [] # print (city_dict[city].keys()) print('%s 有以下区县:' % city) for i in sorted(city_dict[city].keys()): print(i) # 将district按行输出 d.append(i) # 将district按列表排列,放入d列表中 # print(d) district_No = input('请输入需查询区县的编号:(后退:b 退出:q)') print('---------------------------------------------------------') if district_No == 'q': print('查询结束') exit() elif district_No == 'b': break else: for i in d: if district_No in i: district = i print('%s 有这些朋友:'% district) for j in city_dict[city][district]: print (j) print('---------------------------------------------------------') goon = input('查询完成,继续?(Y:继续 其他:结束)') if goon == 'Y': break else: print('查询结束') exit() else: print('区县编号输入错误,请重新输入') while(1): c = [] d = [] for i in sorted(city_dict.keys()): # print (i) # 将city按行输出 c.append(i) # 将city按列表排列,放入c列表中 #print (c) city_No = input('请输入需查询城市的编号:(退出:q)') print('---------------------------------------------------------') if city_No == 'q': # 如果输入q,则退出 print('查询结束') break else: # 如果没有输入q for i in c: # 遍历c列表中的城市 #print (i) if city_No in i: # 如果输入的city_No在遍历的某城市i中 city = i # 把i赋值给city district() #print(i) break else: print('城市编号输入错误,请重新输入') # 如果输入的city_No不在遍历的城市中,重新输入并返回