day01作业及练习题
1. 基于文件存储的用户登录程序(3次登录失败,锁定用户)
示例:
db
alex|123123|3
eric|123123|3
1 # #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 import getpass 4 db = open('db','r') 5 data =db.read() 6 db.close() 7 #print(data) 8 user_list = [] 9 user_str = [] 10 target = '' 11 user_str_list = data.split('\n') 12 #print(user_str_list) 13 for temp in user_str_list: 14 temp =temp.split('|') 15 #print(temp) 16 # dic_list = { 17 # 'name': temp[0], 18 # 'pwd': temp[1], 19 # 'times': temp[2] 20 # } 21 dic_list = { 22 temp[0]:[temp[1],temp[2]] 23 24 } 25 # print(dic_list) 26 user_list.append(dic_list) 27 user_info ={} 28 lock_username = [] 29 num = 0 30 while num < len(user_list): 31 user_info.update(user_list[num]) ##合并字典 32 num += 1 33 #print(user_info) 34 #user_info ={'eric': ['123123', '3'], 'aa': ['123123', '1'], 'alex': ['123123', '0']} 35 while True: 36 username = input("Please input user \033[;34maccount\033[0m:").strip() 37 if len(username) == 0: ##如果输入空继续输入用户名 38 continue 39 if username in user_info.keys(): 40 if int(user_info[username][1]) >3: 41 print("%s user has been locked, please unlock" % username) 42 break 43 for num in range(int(user_info[username][1]),4,1): 44 passwd = getpass.getpass("Please input user \033[;34mpassword\033[0m:").strip() 45 if len(passwd) ==0: 46 continue 47 if passwd == user_info[username][0]: 48 print("Welcome \033[;34m%s\033[0m login system!!!!" % username) 49 exit() 50 else: 51 print("Wrong password, Can try again %r itmes" % (abs(3 - num))) 52 user_info[username][1] = int(user_info[username][1]) +1 53 continue 54 else: 55 #print(user_info) 56 for k, y in user_info.items(): 57 user_str.append((k, y[0], y[1])) 58 num = 0 59 while num < len(user_str): 60 target += str(user_str[num]) ##合并字符串 61 num += 1 62 db_new = target.replace(',', '|').replace(')', '\n').replace("'", "").replace('(', '').replace(' ', '').strip() 63 f2 = open('db', 'w') 64 f2.write(db_new) 65 f2.close() 66 print("\033[;31m您的密码输入错误超过3次,已经锁定\033[0m") 67 exit() 68 else: 69 print ("\033[;31mWrong account %s,retry\033[0m" % username)
a. 元素分类
有如下值集合 v1 = [11,22,33,44,55,66,77,88,99,90],
将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
v2 = {'k1': [],'k2':[] }
#!/usr/bin/python # -*- coding:utf-8 -*- v1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] v2 = { 'k1':[], 'k2':[] } item1 = [] item2 = [] for item in v1: if int(item) > 66: item1.append(item) v2['k1'] = item1 else: item2.append(item) v2['k2'] = item2 print (v2)
b. 功能要求:
v = 2000
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 shopping_cart = [] 4 goods = [ 5 {"name": "电脑", "price": 1999}, 6 {"name": "鼠标", "price": 20}, 7 {"name": "游艇", "price": 2000}, 8 {"name": "美女", "price": 38}, 9 {"name": "汽车", "price": 59999} 10 ] 11 num = input('>>>请输入您的金额:').strip() 12 #num = 2000 13 num = int(num) 14 for val in enumerate(goods): ##获取goods位置 15 id = val[0] 16 product_list = val[1] 17 product_name = val[1]['name'] 18 product_price = val[1]['price'] 19 print (id,":",product_name,product_price) 20 cost = 0 21 while True: 22 choice_product = input('>>>请选择购买的商品编号').strip() 23 if choice_product.isdigit(): 24 choice_product = int(choice_product) 25 if choice_product < len(goods) and choice_product >= 0: 26 p_items = goods[choice_product] 27 #p_name = p_items['name'] 28 #p_money = p_items['price'] 29 #if p_money < num: 30 shopping_cart.append(p_items) 31 print("您购买的商品已经加入购物车如下") 32 cost = 0 33 for p_items in shopping_cart: 34 print(p_items['name'],p_items['price']) 35 cost += p_items['price'] 36 #print(cost) 37 stop = input("是否结账(Y/N):") 38 if stop.upper() == "Y": 39 if num > cost: 40 num -= cost 41 print("您购买的商品清单如下:") 42 for p_items in shopping_cart: 43 print(p_items['name'], p_items['price']) 44 print("\033[32m消费总金额:", cost, "元\033[0m") 45 print("\033[32m您的余额为:", num, "元\033[0m") 46 break 47 else: 48 print("您的余额不足,还差%s" % (abs(cost - num))) 49 break 50 else: 51 print("您选择的商品此商场没有,请选择其他商品!") 52 else: 53 print("请输入正确参数")
c. 用户交互,显示省市县三级联动的选择
dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
}
"河南": {
...
}
"山西": {
..
}
}
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 # c.用户交互,显示省市县三级联动的选择 4 dic = { 5 "河北": { 6 "石家庄": ["鹿泉", "藁城", "元氏"], 7 "邯郸": ["永年", "涉县", "磁县"], 8 }, 9 "黑龙江": { 10 "哈尔滨":["宾县","通河","依兰"], 11 "齐齐哈尔":["富裕县","依安县","拜泉县"] 12 }, 13 "山西": { 14 "太原":["清徐县","阳曲县","娄烦县"], 15 "大同":["天镇县","阳高县","广灵县"] 16 }, 17 "河南":{ 18 "郑州":["中原区","管城回族区","上街区"], 19 "开封市":["龙亭区","鼓楼区","开封县"] 20 } 21 } 22 print('欢迎登陆省市级查询系统') 23 while True: 24 for v in dic.keys(): 25 print(v) 26 #while True: 27 inp = input('>>>请选择您要查询的省份:').strip() 28 while True: 29 for v in dic[inp]: 30 print(v) 31 #while True: 32 city = input('>>>请选择您要查询的城市:').strip() 33 print("您查询的%s省%s市,包含以下县城市" % (inp,city)) 34 for c in dic[inp][city]: 35 print("\033[32m%s\033[0m" % c) 36 stop_city = input(">>>您是否查询%s省的其他城市(Y/N):" % inp) 37 if stop_city.upper() == "Y": 38 continue 39 elif stop_city.upper() == "N": 40 break 41 stop = input(">>>您是否选择其他省份(Y/N):") 42 if stop.upper() == "Y": 43 continue 44 elif stop.upper() == "N": 45 break

浙公网安备 33010602011771号