码迷,mamicode.com
首页 > 其他好文 > 详细

购物车小优化

时间:2018-08-18 17:45:51      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:ppi   else   print   --   ...   enum   display   invalid   ide   

加入time模块,在退出系统前等待2s

技术分享图片
 1 import time
 2 product_list = [
 3     (iphone,5800),
 4     (Mac Pro, 10800),
 5     (Bike, 800),
 6     (Coffee, 31),
 7     (iWatch, 6800),
 8     (ArcherZon Python, 102),
 9 ]
10 salary = input("Enter your salary:")
11 shopping_list = []
12 exit_flag = False
13 while not exit_flag:
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 \031[31m;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("即将打印购物清单,请稍等...")
32                     time.sleep()
33                     print("-----shopping_list-----")
34                     for p in shopping_list:
35                         print(p)
36                     print("Your current balance is \033[31;1m%s\033[0m."%salary)
37                     exit_flag = True
38                 else:
39                     print("\033[31;1minvalid option!\033[0m")
40             else:
41                 print("\033[31;1minvalid option!\033[0m")
42     elif salary == q:
43         print("即将退出,请稍等...")
44         time.sleep(2)
45         exit_flag = True
46     else:
47         print("\033[31;1minvalid option!\033[0m")
View Code

 

购物车小优化

标签:ppi   else   print   --   ...   enum   display   invalid   ide   

原文地址:https://www.cnblogs.com/archerzon/p/9497515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!