首页 > 代码库 > python练习_购物车(简版)

python练习_购物车(简版)

 python练习_购物车(简版)

需求:

  1. 写一个python购物车可以输入用户初始化金额
  2. 可以打印商品,且用户输入编号,即可购买商品
  3. 购物时计算用户余额,是否可以购买物品
  4. 退出结算时打印购物小票

 

以下代码实现的功能与思路:

功能: 

  (1)预算金额控制,只能输入大于0的数字
  (2)商品格式化打印
  (3)选择完成要买的商品后,提示用户再次确认,确认后开始计算用户余额是否大于等于商品价格,价格正确后则加入购物车
  (4)输入q则进行结算,结算时将重复的商品进行合并,显示个数,并计算消费总额和余额

思路:

  (1)商品打印通过嵌套列表实现

  (2)购买商品是只需要比较用户余额和商品价格即可,如果比商品金额打就把这个商品加入到一个列表中

  (3)结算时计算用户购物车列表即可

 

流程图:

 技术分享

使用方法:

  执行环境:Python3.5

  执行方法,执行执行即可

代码:

技术分享
 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 import time
 5 
 6 List_items = [ ["iPhone4",100],["iPhone5",200],["iPhone6",300],["iPhone7",4000],["Python",10000],]
 7 User_shopping_cart = []
 8 
 9 def in_money():
10     ‘‘‘
11     判断用户输入金额的函数
12     ‘‘‘
13     global user_in_money
14     while True:
15         user_in_money = input("Please enter initial funds:").strip()
16         if user_in_money.isdigit():
17             if int(user_in_money) > 0:
18                 while True:
19                     print_lists()
20             else:
21                 print("\033[31mInput Error!\033[0m")
22         else:
23             print("\033[31mInput Error!\033[0m")
24 
25 def print_lists():
26     ‘‘‘
27     获取用户输入的编号,调用结算模块
28     :return:
29     ‘‘‘
30     print("Product List".center(40,"-"))
31     Spaces = " "*2
32     for Product_info in List_items:
33         Underlined = 20-len(Product_info[0])
34         print(Spaces,List_items.index(Product_info)+1,Spaces,Product_info[0],"."*Underlined,Product_info[1])
35     print("-"*40)
36 
37     in_Numbering = input("Please enter the product number,[q]exit billing:").strip()
38     if in_Numbering.isdigit():
39         if int(in_Numbering) > 0 and int(in_Numbering)<= len(List_items):
40             Transaction_Calculations(in_Numbering)
41         else:
42             print("\033[31mThe item number does not exist!\033[0m")
43     else:
44         if in_Numbering == "q":
45             settlement()
46         else:
47             print("\033[31mInput Error!\033[0m")
48 
49 def Transaction_Calculations(Numbering):
50     ‘‘‘
51     加入购物车模块,判断用户余额是否足够购买商品
52     ‘‘‘
53     global user_in_money
54     user_in_money = int(user_in_money)
55     Numbering = int(Numbering)
56     Pu_confirmation = input("Product \033[32m%s\033[0m Whether to add to cart(y/n):"%List_items[Numbering-1][0]).strip()
57     if Pu_confirmation == "y":
58             if user_in_money >= List_items[Numbering-1][1]:
59                 User_shopping_cart.append(List_items[Numbering-1])
60                 user_in_money = user_in_money - List_items[Numbering-1][1]
61                 print("Product %s Added Cart, Current Balance %s¥"%( List_items[Numbering-1][0],user_in_money))
62             else:
63                 print("The balance is insufficient, the commodity price \033[31m%s\033[0m¥,lacks \033[31m%s\033[0m¥"%(List_items[Numbering-1][1],List_items[Numbering-1][1]-user_in_money))
64     else:
65         print("\033[31mNot added to cart\033[0m")
66 
67 def settlement():
68     ‘‘‘
69     结算模块
70     ‘‘‘
71     if len(User_shopping_cart) == 0:
72         print("Shopping Cart There are no products, thank you patronage goodbye")
73         exit()
74     else:
75         print("Shopping list".center(50,"-"))
76         consumption = 0
77         new_user = []
78         [new_user.append(i) for i in User_shopping_cart if not i in new_user]
79         for user_cart in new_user:
80             number = User_shopping_cart.count(user_cart)
81             settlement_un = 15-len(user_cart[0])
82             to_settlement_un = 25 - settlement_un - len(str(user_cart[1])) - len(user_cart[0])
83             consumption += user_cart[1]*number
84             print(" "*5,user_cart[0],"."*settlement_un,user_cart[1],"","."*to_settlement_un,"%s个"%(number))
85         times = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
86         print(times.center(50,"-"))
87         print("Shopping cost \033[32m%s\033[0m¥, Current balance \033[32m%s\033[0m¥,Thank you to patronize!".center(50,"-")%(consumption,user_in_money))
88         exit()
89 
90 if __name__ == "__main__":
91     in_money()
购物车

 

python练习_购物车(简版)