python-购物车

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-购物车相关的知识,希望对你有一定的参考价值。

购物车功能如下:

1. 输入收入多少,购买商品

2. 打印购物清单,根据清单选择商品;

3. 结算,打印购物清单及总金额

 1 # -*- coding:utf-8 -*-
 2 # LC
 3 
 4 goods=[[1,iphone,5800],[2,Mac Pro,12000],[3,apple,30],[4,egg,5],[5,pig,2000]]         #商品列表
 5 salary = input(Please input your salary:)                                                       #输入薪资
 6 shopping_cart = []                                           #购物车列表
 7 shopping_money=[]                                           #消费金额列表
 8 salary=int(salary)
 9 balance=int(salary)                                         #消费余额,未开始消费等于薪资
10 shopping_total_money=0
11 while True:
12     for i in goods:                                         #打印商品清单
13         print(i)
14     select = input("Please input your select,or input ‘q‘ to quit:")        #输入购买清单选项,按"q"退出
15     if select=="q":
16         if balance==salary:                                                         #判断是否购买了东西
17             print("Do not buy any thing")
18             break
19         else:
20             print("You have been bought below:")
21             for k in shopping_cart:
22                 print(k)
23             print("Total Comsume:",shopping_total_money)
24             break
25     else:
26         select_sequence = int(select)
27         goods_max_sequence = int(goods[-1][0])
28         if select_sequence>goods_max_sequence:                                        #如果输入序列号大于商品序列号,提示输入错误
29             print("------------Warning-------------")
30             print("Your selected is not correctly,please retry")
31             print("------------Warning-------------")
32         else:
33             goods_price = int(goods[select_sequence - 1][2])
34             if goods_price<=balance:                                                #如果余额大于购买商品的金额,则加入购物车
35                 shopping_cart.append(goods[select_sequence-1])
36                 shopping_money.append(goods_price)                              #消费金额加入消费金额列表
37                 shopping_total_money = sum(shopping_money)                      #计算商品总金额
38                 shopping_total_money=int(shopping_total_money)
39                 balance = salary - shopping_total_money                         #余额计算
40             else:
41                 print(You have not enough money!)                 #余额不足,提示购买清单
42                 for k in shopping_cart:
43                     print(k)
44                 print("Total Comsume:", shopping_total_money)
45                 break

 

以上是关于python-购物车的主要内容,如果未能解决你的问题,请参考以下文章

python 购物车代码

Python简单的购物车小代码

Python小代码_3_购物车

常用python日期日志获取内容循环的代码片段

python 有用的Python代码片段

Python 向 Postman 请求代码片段