Python 购物车
-
需求
- 用户名和密码存放于文件中,格式为:xxx|xxx
- 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
-
流程图
- Python代码实现
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # 商城购物车 4 product_list = [ 5 [‘Iphone7 Plus‘,6500], 6 [‘Iphone8 ‘,8200], 7 [‘MacBook Pro‘,12000], 8 [‘Python Book‘,99], 9 [‘Coffee‘,33], 10 [‘Bike‘,666], 11 [‘pen‘,2] 12 ] 13 shopping_cart = [] 14 f = open(‘user.txt‘,‘r‘) 15 lock_file = f.readlines() 16 f.close() 17 count=0 18 user_list={} 19 while True: 20 if count == 3: 21 print("用户名输入次数到达3次限制") 22 break 23 for i in lock_file: 24 i=i.strip() 25 user_list[i.split(‘|‘)[0]]={‘password‘:i.split(‘|‘)[1]} 26 user_name=input("请输入您的用户名>>:") 27 if user_name not in user_list: 28 print("用户名错误") 29 count+=1 30 if user_name in lock_file: 31 print("用户名已锁定,请联系管理员!") 32 exit() 33 if user_name in user_list: 34 user_password=input("请输入您的密码>>: ") 35 if user_password == user_list[user_name][‘password‘]: 36 print("欢迎登录电子商城") 37 while True: 38 salary = input("请输入您的工资:") # 输入金额 39 if not salary.isdigit(): # 判断输入的salary是不是数字 40 print("由于您的输入的工资不合法,请再次输入金额") # 输入金额不合法 41 continue 42 else: 43 salary = int(salary) # 把输入的数字转成整形 44 break 45 while True: 46 print(">> 欢迎来到电子商城 <<") 47 for index, i in enumerate(product_list): # 循环商品列表,商品列表索引 48 print("%s.\t%s\t%s" % (index, i[0], i[1])) # 打印商品列表,显示商品列表索引 49 choice = input(">>请输入商品序号或输入 exit 退出商城>>: ").strip() 50 if len(choice) == 0: # 判断输入字符串是否为空和字符串长度 51 print(‘-->您没有选择商品<--‘) 52 continue 53 if choice.isdigit(): # 判断输入的choice是不是一个数字 54 choice = int(choice) # 把输入的字符串转成整型 55 if choice < len(product_list) and choice >= 0: # 输入的整数必须小于商品列表的数量 56 product_item = product_list[choice] # 获取商品 57 if salary >= product_item[1]: # 拿现有金额跟商品对比,是否买得起 58 salary -= product_item[1] # 扣完商品的价格 59 shopping_cart.append(product_item) # 把选着的商品加入购物车 60 print("添加 \033[32;1m%s\033[0m 到购物车,您目前的金额是 61 \033[31;1m%s\033[0m" % (product_item[0], salary)) 62 else: 63 print("对不起,您的金额不足,还差 \033[31;1m%s\033[0m" % (product_item[1] - salary,)) 64 else: 65 print("-->没有此商品<--") 66 elif choice == "exit": 67 total_cost = 0 68 print("您的购物车列表:") 69 for i in shopping_cart: 70 print(i) 71 total_cost += i[1] 72 print("您的购物车总价是: \033[31;1m%s\033[0m" % (total_cost,)) 73 print("您目前的余额是:\033[31;1m%s\033[0m" % (salary,)) 74 break 75 break 76 else: 77 print("密码错误") 78 count += 1 79 if count == 3 : 80 print("您输入的密码错误次数已达3次,将锁定您的用户!") 81 f = open(‘blacklist.txt‘,‘w‘) 82 f.write(‘%s‘%user_name) 83 f.close() 84 break 85 86 while True: 87 salary = input("请输入您的工资:") # 输入金额 88 if not salary.isdigit(): # 判断输入的salary是不是数字 89 print("由于您的输入的工资不合法,请再次输入金额") # 输入金额不合法 90 continue 91 else: 92 salary = int(salary) # 把输入的数字转成整形 93 break 94 while True: 95 print(">> 欢迎来到电子商城 <<") 96 for index, i in enumerate(product_list): # 循环商品列表,商品列表索引 97 print("%s.\t%s\t%s" % (index, i[0], i[1])) # 打印商品列表,显示商品列表索引 98 choice = input(">>请输入商品序号或输入 exit 退出商城>>: ").strip() 99 if len(choice) == 0: # 判断输入字符串是否为空和字符串长度 100 print(‘-->您没有选择商品<--‘) 101 continue 102 if choice.isdigit(): # 判断输入的choice是不是一个数字 103 choice = int(choice) # 把输入的字符串转成整型 104 if choice < len(product_list) and choice >= 0: # 输入的整数必须小于商品列表的数量 105 product_item = product_list[choice] # 获取商品 106 if salary >= product_item[1]: # 拿现有金额跟商品对比,是否买得起 107 salary -= product_item[1] # 扣完商品的价格 108 shopping_cart.append(product_item) # 把选着的商品加入购物车 109 print("添加 \033[32;1m%s\033[0m 到购物车,110 您目前的金额是 \033[31;1m%s\033[0m"%(product_item[0],salary)) 111 else: 112 print("对不起,您的金额不足,还差 \033[31;1m%s\033[0m" % (product_item[1] - salary,)) 113 else: 114 print("-->没有此商品<--") 115 elif choice == "exit": 116 total_cost = 0 117 print("您的购物车列表:") 118 for i in shopping_cart: 119 print(i) 120 total_cost += i[1] 121 print("您的购物车总价是: \033[31;1m%s\033[0m" % (total_cost,)) 122 print("您目前的余额是: \033[31;1m%s\033[0m" % (salary,)) 123 break
时间: 2024-08-02 23:03:44