python-购物车优化二

在退出系统,打印购物清单后,wait 2s再退出

 1 #Author:Archer Zon
 2 import time
 3 product_list = [
 4     ('iPhone X', 5800),
 5     ('Mac Pro', 10800),
 6     ('Bike', 800),
 7     ('Coffe', 31),
 8     ('iWatch', 9800),
 9     ('Archerzon Python', 120),
10 ]
11 salary = input("\033[36;1mEnter your salary:\033[0m")
12 shopping_list = []
13 exit_flag = False
14 if salary.isdigit():
15     salary = int(salary)
16     while not exit_flag:
17         for index,item in enumerate(product_list):
18             print(index,item)
19         user_choice = input("选择你要购买的商品:")
20         if user_choice.isdigit():
21             user_choice = int(user_choice)
22             if user_choice < len(product_list) and user_choice >= 0:
23                 p_item = product_list[user_choice]
24                 if p_item[1] < salary:
25                     shopping_list.append(p_item)
26                     salary -= p_item[1]
27                     print("You have bought \033[31;1m%s\033[0m.Your current balance is \033[31;1m%s\033[0m."%(p_item[0],salary))
28                 else:
29                     print("余额不足,仅剩\033[31;1m%s\033[0m.请及时充值!"%salary)
30         elif user_choice == 'q':
31             print("------shopping list------")
32             print("正在打印购买清单,请稍等...")
33             time.sleep(2)
34             for p in shopping_list:
35                 print(p)
36             print("Yout current balance is \033[31;1m%s\033[0m"%salary)
37             print("打印完毕,即将退出系统...")
38             time.sleep(2)
39             exit_flag = True
40         else:
41             print("\033[31;1mInvalid option!\033[0m")
42 elif salary == 'q':
43     print("正在退出系统,请稍等...")
44     time.sleep(2)
45     pass
View Code

 

转载于:https://www.cnblogs.com/archerzon/p/9505048.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值