首页 > 代码库 > 购物列表

购物列表

 1 #!/usr/bin/env python
 2 #_*_ coding:utf-8 _*_
 3 Goods_list = [
 4     ("iPhone6", 5888),
 5     ("MacPro", 9888),
 6     ("bike", 888),
 7     ("Coffee", 28),
 8 ]
 9 
10 def Goods():
11     for i in enumerate(Goods_list, 1):
12         print(i[0], i[1])
13 
14 def Choces():
15     print( ‘‘‘
16     \t\t\t\033[31;1m欢迎来到购物商城\033[0m
17     \t\t\t1.购买商品
18     \t\t\t2.查看购物车
19     \t\t\t3.退出
20     ‘‘‘)
21 shopping_cart = []
22 gz = input("你的工资:")
23 if gz.isdigit():  # 是不是数字
24     gz = int(gz)
25 while True:
26     Choces()
27     chocesID = input("\033[32;1m你的选择:\033[0m")
28     if chocesID == "1":
29         Goods()
30         user_choice = input("你的要买的商品:")
31         user_choice = int(user_choice)
32         if user_choice < len(Goods_list) + 1 and user_choice >= 1:
33             p_item = Goods_list[user_choice - 1]
34             if p_item[1] <= gz:
35                 shopping_cart.append(p_item)
36                 gz -= p_item[1]
37                 print(gz)
38                 print("购买%s成功,余额为\033[32;1m%s\033[0m" % (p_item, gz))
39             else:
40                 print("余额不足")
41         else:
42             print("选择不存在")
43     if chocesID == "2":
44         print(shopping_cart)
45     if chocesID == "3":
46         print("\t\t\t--------购物列表--------")
47         for i in shopping_cart:
48             print("\t\t\t",i)
49         exit("你的余额为%s"%gz)

 

购物列表