苹果IT家园

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

1、购物车程序

 1 product_list = [
 2      ('Iphone',5800),
 3      ('Mac Pro',9800),
 4      ('Bike',800),
 5      ('Watch',10600),
 6      ('Coffee',30),
 7      ('python',120),
 8 ]
 9 shooping_list = []
10 salary = input("Input your salary:")
11 if salary.isdigit():
12     salary = int(salary)
13     while True:
14         #for item in product_list:
15            # print(product_list.index(item),item)
16         for index,item in enumerate(product_list): #下标,数据,enumerate取列表的下标
17             print(index,item)
18         user_choice = input("选择要买的商品:")
19         if user_choice.isdigit():
20             user_choice = int(user_choice)
21             if user_choice < len(product_list) and user_choice >= 0:
22                 p_item = product_list[user_choice]
23                 if p_item[1] <= salary: #买得起
24                     shooping_list.append(p_item)
25                     salary -= p_item[1]
26                     print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary))
27                 else:
28                     print("\033[41;1m你的余额只剩[%s]啦,不能买了\033[0m"% salary)
29             else:
30                 print("product code [%s] is not exist!"% user_choice)
31         elif user_choice == 'q':
32             print("-----------shopping list-----------")
33             for p in shooping_list:
34                 print(p)
35             print("Your current balance:",salary)
36             exit()
View Code

 

posted on 2017-06-11 09:18  苹果IT家园  阅读(128)  评论(0)    收藏  举报