python 购物车+用户认证程序
创建文件a.txt,b.txt.c.txt用于存放应该持续保存的信息
a.txt :用户密码输入错误3次就锁定
b.txt :购物时的活动,每个用户只能参与一次
c:txt :购物完后的发票在这里查看
购物车程序:
1 ready = 0 2 salary = input("how much is you salary:") 3 out = 0 4 #count = 0 5 if salary.isdigit(): 6 salary = int(salary) 7 else: 8 exit("input string not digital....exit") 9 list = [["精选猪肉3斤", 100], ["牛肉3斤", 200], ["鸡肉三斤", 50]] 10 shoppingcar = [] 11 while True: 12 print("----------------------------------------------------------------------------") 13 print("Welcome to xiaodai store") 14 print("\033[31;1m目前正在做活动,购买商品满500的话就赠送一个精美的杯子哦!!!(超市内按h咨询)每人限1次 \033[0m") 15 for i in enumerate(list): 16 info_l = i[0] 17 info_n = i[1][0] 18 info_m = i[1][1] 19 print(info_l, info_n, info_m) 20 user_c = input("please input your choose(h/help):") 21 if user_c.isdigit(): 22 user_c = int(user_c) 23 if user_c < len(list) and user_c >= 0: 24 user_c = int(user_c) 25 money = list[user_c][1] 26 shoppingcar_1 = list[user_c] 27 ready = ready + money 28 if salary >= money: 29 shoppingcar.append(shoppingcar_1) 30 print(shoppingcar_1, "add in then shoppingcar.....") 31 salary -= money 32 else: 33 print(" ----------------------") 34 print("|your money not enough:|", salary) 35 print(" ----------------------") 36 else: 37 print("没有这个商品编号") 38 elif user_c == "h" or user_c == help or user_c == "H": 39 print("q/quit quit store") 40 print("c/check check shopping car and salary") 41 print("0-2 choice of goods") 42 print("购买完商品后,输入[F]键来参加活动(满500赠送精美杯子)") 43 print("l/list 查看发票,购买完并退出超市后才能通过此方法看到发票") 44 elif user_c == "q" or user_c == "quit" or user_c == "Q": 45 print("your shoppingcar:", shoppingcar) 46 print("Be left over how much money:", salary) 47 time_1 = '%Y-%m-%d-%X' 48 time_2 = time.strftime(time_1) 49 with open('b.txt', 'a', encoding='utf-8') as f2: 50 f2.write("发票:%s用户 在 %s 购买了 %s 共消耗%s 元 \n" %(username,time_2,shoppingcar,ready)) 51 exit("quit....") 52 elif user_c == 'l'or user_c =='list': 53 with open('b.txt','r+',encoding='utf-8')as f3: 54 print(f3.read()) 55 elif user_c == "c" or user_c == "check" or user_c == "C": 56 print("Be left over how much money:", salary) 57 print(shoppingcar) 58 elif user_c == "f" or user_c == "F": 59 if ready >= 500 : 60 with open('c.txt','r+',encoding='utf-8') as f4: 61 aready=f4.read() 62 if username not in aready: 63 print("你的满额任务达成,获得精美杯子一个..................") 64 shoppingcar.append(['精美杯子', 0]) 65 if len(aready) != 0 : 66 f4.write('\n'+username) 67 else: 68 f4.write(username) 69 else: 70 print('你已经领取过奖励了,你的购物车:%s'%shoppingcar) 71 continue 72 else: 73 print("你没有满足活动的需求") 74 else: 75 exit("input error")
所有代码:
import time,sys def shopping(): "'购物车程序,进入时输入有多少钱,然后进行商品购物,消费满500后选择接受礼品,礼品每个用户只能接受一次,接受过的用户存放在c.txt文件中,购物完毕后打印发票,发票存储在b.txt文件中," ready = 0 salary = input("how much is you salary:") out = 0 #count = 0 if salary.isdigit(): #判断salary是否为数字 salary = int(salary) else: exit("input string not digital....exit") list = [["精选猪肉3斤", 100], ["牛肉3斤", 200], ["鸡肉三斤", 50]] shoppingcar = [] while True: print("----------------------------------------------------------------------------") print("Welcome to xiaodai store") print("\033[31;1m目前正在做活动,购买商品满500的话就赠送一个精美的杯子哦!!!(超市内按h咨询)每人限1次 \033[0m") for i in enumerate(list): #enumerate可以在读取数据时添加一个编号在第一位,如[0,"精选猪肉3斤", 100] info_l = i[0] info_n = i[1][0] info_m = i[1][1] print(info_l, info_n, info_m) user_c = input("please input your choose(h/help):") if user_c.isdigit(): user_c = int(user_c) if user_c < len(list) and user_c >= 0: user_c = int(user_c) money = list[user_c][1] shoppingcar_1 = list[user_c] ready = ready + money if salary >= money: shoppingcar.append(shoppingcar_1) print(shoppingcar_1, "add in then shoppingcar.....") salary -= money else: print(" ----------------------") print("|your money not enough:|", salary) print(" ----------------------") else: print("没有这个商品编号") elif user_c == "h" or user_c == help or user_c == "H": print("q/quit quit store") print("c/check check shopping car and salary") print("0-2 choice of goods") print("购买完商品后,输入[F]键来参加活动(满500赠送精美杯子)") print("l/list 查看发票,购买完并退出超市后才能通过此方法看到发票") elif user_c == "q" or user_c == "quit" or user_c == "Q": print("your shoppingcar:", shoppingcar) print("Be left over how much money:", salary) time_1 = '%Y-%m-%d-%X' time_2 = time.strftime(time_1) with open('b.txt', 'a', encoding='utf-8') as f2: f2.write("发票:%s用户 在 %s 购买了 %s 共消耗%s 元 \n" %(username,time_2,shoppingcar,ready)) exit("quit....") elif user_c == 'l'or user_c =='list': with open('b.txt','r+',encoding='utf-8')as f3: print(f3.read()) elif user_c == "c" or user_c == "check" or user_c == "C": print("Be left over how much money:", salary) print(shoppingcar) elif user_c == "f" or user_c == "F": if ready >= 500 : with open('c.txt','r+',encoding='utf-8') as f4: aready=f4.read() if username not in aready: print("你的满额任务达成,获得精美杯子一个..................") shoppingcar.append(['精美杯子', 0]) if len(aready) != 0 : #len可以打印这个变量中是否有数据,他会显示这个文件的字数,添加这个判断是格式问题,如果文件中没有数据,责直接将数据写入进去,不加空格,如果存在数据,责在插入数据前先执行回车. f4.write('\n'+username) #在写入内容是在输入回车,不能用逗号来区分,用加号 else: f4.write(username) else: print('你已经领取过奖励了,你的购物车:%s'%shoppingcar) continue else: print("你没有满足活动的需求") else: exit("input error") def auth(): '''慢慢的显示验证中''' for i in range(1): sys.stdout.write("验") sys.stdout.flush() time.sleep(0.3) sys.stdout.write("证") sys.stdout.flush() time.sleep(0.3) sys.stdout.write("中") sys.stdout.flush() time.sleep(0.3) sys.stdout.write("请") sys.stdout.flush() time.sleep(0.3) sys.stdout.write("稍") sys.stdout.flush() time.sleep(0.3) sys.stdout.write("等") sys.stdout.flush() time.sleep(0.3) user_list={ 'xiaoming':{'xiaoming1'}, 'xiaogang':{'xiaogang1'}, 'xiaomei':{'xiaomei1'} } with open('a.txt','r+',encoding='utf-8')as f: lock_list=f.read() count=0 username = input(' please input your user name:') for l in range(100): passwd = input('\033[31;3m please input your passwd:\033[1m') for i in user_list: auth() print('\n') if username in lock_list : print("用户已被锁定") break elif username not in user_list : print('用户不存在') username = input(' please input your user name:') break else: a=user_list[username] if passwd in a : print('登录成功') shopping() elif count >= 3: print("密码错误次数过多,用户%s被锁定,如需解锁,请联系管理员"%username) f.write(username) f.write('\n') exit('quit...') else: print('密码错误登录失败,请重试') count = count + 1 break #为了确保如"密码错误登录失败,请重试"提示只提示一次,而不是按照for循环的提示多次,当执行完后就退出for循环,退出后又继续加载这个循环。
这个call机是我的唉