[后端-Python]-项目练习集

 1 1.
 2 '''
 3 knowledge_point: 
 4 1.练习if...else;
 5 2.练习while;
 6 3.练习列表的切片取值;
 7 
 8 requirements:
 9     购物车:
10     1.程序启动后,输入工资,打印商品列表
11     2.提示用户根据商品编号购买产品
12     3.选择商品后检测余额是否足够,扣款或提醒余额不足
13     4.随时退出,退出时打印已经购买的商品和余额
14 
15 code:
16 '''
17 product_li = [
18     ("mac pro", 12000),
19     ("hp", 5000),
20     ("iphone", 6000),
21     ("huawei", 5000),
22     ("gopro", 4000)
23 ]
24 salary = input("input salary: ")
25 shopping_li = []
26 if salary.isdigit():
27     salary = int(salary)
28     while True:
29         for i, item in enumerate(product_li):
30             print(i+1, item)
31         choice_num = input('输入编号购买:')
32         if choice_num.isdigit():
33             choice_num = int(choice_num)
34             if choice_num <= len(product_li) and choice_num >= 0:
35                 paid_price = product_li[choice_num-1][1]
36                 if paid_price <= salary:
37                     paid_item = product_li[choice_num - 1]
38                     print("you can pay")
39                     shopping_li.append(paid_item)
40                     salary -= paid_price
41                     print("surplus is \033[32;1m{}\033[0m".format(salary))
42                 else:
43                     print('\033[41;1m not enough money {}\033[0m'.format(salary))
44             else:
45                 print("product is not exist")
46         elif choice_num == "q":
47             # print("exited")
48             print('---------shopping_list_as_follows-------------')
49             for p in shopping_li:
50                 print(p)
51             print("current balance is {}".format(salary))
52             exit() #退出
53         else:
54             print("invalid choice")
View Code

 

posted @ 2019-08-13 15:10  mezc  阅读(308)  评论(0编辑  收藏  举报