Python 购物车

Python 购物车

  • 需求

  1. 用户名和密码存放于文件中,格式为:xxx|xxx
  2. 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序
  3. 允许用户根据商品编号购买商品
  4. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  5. 可随时退出,退出时,打印已购买商品和余额
  • 流程图

  • 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

Python 购物车的相关文章

python购物车小案例

python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果.# 1.客户输入相应序号和对应数量后将该商品加入购物车# 2.付款时检查客户是否有支付能力(结算金额<=100)# 3.客户输入Q/q退出选购 lis = [{'name': '苹果', 'price': 8}, {'name': '香蕉', 'price': 5}, {'name': '芒果', 'price': 15}

17.python购物车程序作业

购物车程序作业需求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4.可随时退出,退出时,打印已购买商品和余额 5.在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示 6.用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买 7.允许查询之前的消费记录 代码如下: # Author:pe

python 购物车和三级菜单

程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 #-*- coding:utf8 -*- shopping_list=[] product_list = [ ('Iphone',5800), ('Mac Pro',9800), ('Bike',800), ('Watch',10600), ('Coffee',31), ('Alex Python

Python购物车模拟

1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4.可随时退出,退出时,打印已购买商品和余额 5.在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示 6.用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买 7.允许查询之前的消费记录 product_list = [ ('Iphone',5

python购物车小程序

要求:1.用户输入工资后展示商品列表2.根据商品编号选择商品3.选择商品后打印商品清单以及剩余工资代码如下:# coding=utf-8product_list = [ ('iphone',5800), ('mac pro',9800), ('bike',800), ('watch',10600), ('coffee',31), ('Alex python',120),]shopping_list = []#购物车salary = input("Input your salary:")

Python购物车程序

#!/usr/bin/env python #_*_ coding:utf-8 _*_#Author:LiuJindong #datetime:2018/6/11 16:34 '''此程序为购物车程序,包括用户入口.商家入口.用户入口:已购商品.余额记录.商家入口:可以添加商品.修改商品价格.''' import time,sys def Business_entrance(ProductList): f1=open(ProductList,'r') Product={} for line in

Python 购物车练习 2.0

product_list = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicycle', 1500]] shopping_cart = [] while True: salary = input("Salary(整数) = ") if salary.isdigit(): salary = int(salary) break else: print('请输入正确的数字

Python购物车的实现课程

需求: 1.用户输入工资收入 2.打印商品列表 3.用户选择商品,不断的加入购物车 4.检测用户余额,直接捐款,不足提示余额不足 5.允许主动退出,退出时,打印已购商品列表 重点方法: 打印列表下标的方法: a=['alex','sys','root','admin'] >>> for index,i in enumerate(a):...     print(index,i) 0 alex1 sys2 root3 admin 1 #!/usr/bin/env python3 2 # -

Python购物车实现

salary=int(input("please input your salary:"))product_list=[['iphone',5299],['coffee',30],['bike',299],['vivo x9',2499],['cake',40],['book',99]]product_car={}total_cost=0 while True:print('--------可以购买的商品如下--------')for number in range(len(produ