简单的购物系统
文件说明:
user_lock:保存已被锁定用户账号信息
salary:保存用户的余额
功能测试账号:ylw 密码:ylwa zhangchao属于黑名单的
功能需求:
1、启动程序后,输入用户名密码后,如果是用户名输入正确则密码输入错误三次锁定账户,
如果用户名输入不正确提示输入正确用户名。
2、登入成功之后打印商品列表,允许用户根据商品编号购买商品。
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒充值在购买。
4、购买成功,系统会提示已购买的商品和余额。
5、如果不想再购买商品就选择购买完成,系统将会显示出你所购买的所有商品和消费的余额和剩余的余额。
1 #/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:ylw 4 5 count = 0 6 user = 'ylw' 7 password = 'ylwa' 8 9 # f1 = open('user','r') 10 # data = f1.read() 11 # f1.close() 12 13 f = open('user_lock','a') 14 f.close() 15 16 f = open('salary','a') # 创建用户余额存放文件 17 f.close() 18 19 f = open('user_lock','r') 20 lock_list = f.readlines() 21 f.close() 22 lock = [] 23 24 name = input('请输入用户名:') 25 for i in lock_list: 26 line = i.strip("\n") 27 lock.append(line) 28 if name in lock: 29 print("您的帐号已锁定!") 30 exit() 31 else: 32 if name == user: 33 while count < 3: 34 pwd = input("登入密码:") 35 if name == user and pwd == password: 36 print("欢迎来到网上商城!") 37 break 38 else: 39 print("账号密码不匹配!") 40 count += 1 41 if count == 3: 42 print("您的密码输错三次,已被锁定!") 43 f = open('aaa.txt','w+') 44 li = ['%s'%user] 45 f.writelines(li) 46 f.close() 47 exit() 48 else: 49 print("您的帐号输错三次,已被锁定!") 50 f = open('user_txt','w+') 51 li = ['%s'%user] 52 f.close() 53 else: 54 print("用户不存在,请输入正确用户") 55 exit() 56 57 shopping_list = [] 58 goods = [] 59 price = [] 60 # f1 = open('goods','r') 61 # file_list = f1.read() 62 # f1.close() 63 product_list = [ 64 ['Iphone',5800], 65 ['Mac Pro',9800], 66 ['Bike',800], 67 ['美女',998], 68 ['汽车',3100], 69 ['银角',120], 70 ] 71 f1 = open("salary","r") 72 file_list = f1.readlines() 73 f1.close() 74 salary = [] 75 if file_list: 76 print("product_list") 77 else: 78 f2 = open("salary","w") 79 f2.write("0") 80 f2.close() 81 f1 = open("salary","r") 82 fil_list = f1.readlines() 83 f1.close() 84 for i in fil_list: 85 lin = i.strip("\n") 86 salary.append(lin) 87 88 salary = int (salary[0]) 89 90 while True: 91 for j in range(1): 92 print("----------商品列表---------") 93 for i,ele in enumerate(product_list): 94 print(i,ele[0],ele[1]) 95 var = (input("[充值:t 余额:b 购买完成:y 退出:q]\n请输入商品的序列号:")) 96 if var.isdigit(): 97 var = int(var) 98 if var >=0 and len(product_list) > var: 99 p = product_list[var] 100 if p[1] <=salary: 101 shopping_list.append(p) 102 goods.append(p[0]) 103 price.append(p[1]) 104 salary = salary -p[1] 105 print("您购买的%s已加入购物车,您的余额还剩%s。"%(p[0],salary)) 106 else: 107 print("您的余额不足(余额:%s),请充值后购买商品(充值:t)" %salary) 108 else: 109 print("输入有误,请重新输入商品编号") 110 continue 111 elif var == "t": 112 num1 = input("请输入充值金额:") 113 if num1.isdigit(): 114 num1 = int(num1) 115 salary = salary + num1 116 print("您现在的余额为:",salary) 117 else: 118 print("请输入正确的充值金额:") 119 continue 120 elif var == "q": 121 exit() 122 elif var == "b": 123 print("您当前的余额为:%s"%salary) 124 elif var == "y": 125 print("商品列表") 126 goods.sort() 127 s = set(goods) 128 for item in s: 129 print("%s -- %d"%(item,goods.count(item))) 130 sum = 0 131 for j in price: 132 sum = sum + j 133 print("您的总计消费:%s,余额为:%s"%(sum,salary)) 134 print("欢迎下次光临,古德拜!") 135 f = open("salary","w+") 136 la = ['%s'%salary] 137 f.writelines(la) 138 f.close() 139 exit() 140 else: 141 print("请输入正确的商品编码。") 142 continue