[后端-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

 

转载于:https://www.cnblogs.com/mezc/p/11346046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值