简单的python购物车

             这几天,一直在学python,跟着视频老师做了一个比较简单的python购物车,感觉不错,分享一下

 

 1 products = [['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]]
 2 
 3 shopping_cart = []
 4 #run_flag = True #标志位
 5 exit_flag = False
 6 while not exit_flag:
 7     print("---------商品列表------------")
 8     for index,p in enumerate(products):
 9         print("%s, %s   %s"  %(index,p[0],p[1]  ))
10 
11     choice = input("请输入你要买的商品编号:")
12     if choice.isdigit():  #判断是不是str类型
13         choice = int(choice)
14         if choice >= 0 and choice < len(products):
15           shopping_cart.append(products[choice])
16           print("Add product %s into shopping_cart." %(products[choice]))
17         else:
18           print("该商品的编号不存在")
19     elif choice == 'q':
20         if len(shopping_cart) >0:
21           print("-------你已经购买以下商品--------")
22           for index,p in enumerate(shopping_cart) :
23              print("%s, %s   %s" % (index, p[0], p[1]))
24 
25              #break
26              #run_flag = False
27              exit_flag = True

 

第二个是我在上网找来的,不过改优化了一点点

 

   

 1 #!/usr/bin/env.python
 2 # -*- coding: utf-8 -*-
 3 """
 4 -------------------------------------------------
 5  File Name:  shopping
 6  Description :
 7  Author :  lwh
 8  date:   2019/2/28
 9 -------------------------------------------------
10 
11 -------------------------------------------------
12 """
13 
14 #定义一个商品的列表
15 product_list = [
16     ('iphone',5000),
17     ('coffee',31),
18     ('bicyle',888),
19     ('iwatch,2666'),
20     ('Mac Pro',15000),
21     ('book',88)]
22 
23 shopping_list = [] #空列表,存放购买的商
24 
25 money = input("请输入你的工资:")
26 if money.isdigit(): # isdigit() 用来检#检测字符串是否只由数字组成,是则返回True,否则False
27     money = int(money)
28 
29     while True:
30         for index,i in enumerate(product_list):  #index作为下标索引
31             # enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
32             print(index,i[0],i[1])  #qi'qin
33         user_choice = input("请输入你要购买的商品: ")
34         if user_choice.isdigit() :    #判断
35                 user_choice = int(user_choice)
36                 if user_choice < len(product_list) and user_choice >= 0:
37                     product_choice = product_list[user_choice]
38                     if product_choice[1] < money: #买得起
39                        shopping_list.append(product_choice) # 买得起,就放入购物车
40                        money -= product_choice[1]
41                        print("添加 %s 到你的购物车了,你的余额为 : \033[31;1m%s\033[0m" % (product_choice, money))
42                     else:
43                         print("\033[41;1m你的余额只剩%s啦,不够买该商品,请重新选择!\033[0m" % money)
44                         print("-----------购 物 车------------")
45                         for s_index, s in enumerate(shopping_list):
46                            print(s_index,s)
47                         print("-----------商  品------------")
48                         print("你的余额为:\033[31;1m%s\033[0m" % money)
49                 else:
50                     print("没有这个商品")
51         elif user_choice == "q" :
52                 print("-------购 物 清 单---------")
53                 for s_index,s in enumerate(shopping_list):
54                      print(s_index,s)
55                 print("-----------购 物 清 单------------")
56                 print("你的余额为 : \033[31;lm%s\033[0m" % money)
57                 exit()
58         else:
59                 print("输入错误,没有这个商品,请重新输入: ")
60     else:
61         print("输入错误,请重新输入: ")

 

 

 
 
第二个购物车原码网站:https://www.cnblogs.com/cxylff/p/8468500.html

猜你喜欢

转载自www.cnblogs.com/lanyincao/p/10453908.html
今日推荐