模拟购物车流程python脚本

模拟购物车流程python脚本

 

 1 #!/usr/bin/env python
 2 #-*- coding: utf-8 -*-
 3 #__author__ = "XZ"
 4 
 5 
 6 product_list = [
 7     ('Iphone', 5800),
 8     ('Mac Pro', 9800),
 9     ('Bike', 800),
10     ('Watch', 10600),
11     ('Coffee', 31),
12     ('Alex Python', 120),
13 ]
14 shopping_list = []
15 salary = input("Input your salary:")
16 if salary.isdigit():
17     salary = int(salary)
18     while True:
19         for index,item in enumerate(product_list):  #通过enumerate获取索引值
20             print(index,item)
21         user_choice = input("选择要买嘛?>>>:")
22         if user_choice.isdigit():
23             user_choice = int(user_choice)
24             if user_choice < len(product_list) and user_choice >=0:
25                 p_item = product_list[user_choice]
26                 if p_item[1] <= salary: #买得起!
27                     salary -= p_item[1]
28                     shopping_list.append(p_item)
29                     print(shopping_list)    #验证购物车列表里的元素是什么样的---测试使用
30                     print("Added %s into shopping cart your, current balance is \033[31;1m%s\033[0m"  %(p_item,salary)) #通过红色字体提示当前余额!
31                 else:
32                     print("\033[41;1m你的余额只剩[%s]啦。。。还买个毛线\033[0m" % salary)  #通过字体的红色背景警告提示
33             else:
34                 print("\033[41;1mProduct code [%s] is not exist !\033[0m"% user_choice)   #通过字体的红色背景警告提示
35         elif user_choice == 'q':
36             print("--------------shopping list---------------")
37             for p in shopping_list:
38                 print(p)
39             print("Your current balance is :%s" (salary))
40             exit()
41 
42         else:
43             print("\033[41;1mInvalid option ! 请输入十进制数值代表的商品编号!\033[0m")  #通过字体的红色背景警告提示

 

####################################

print显示字体颜色代码整理:

格式:

” XXX\033[31;1mYY\033[0m”

红色字体的31为表示字体颜色的颜色代码,31表示输出的YY字体为红色

字体颜色代码整理如下:

29 高亮原色

30 加粗高亮

31 红色

32 绿色

33 黄色

34 蓝色

35 紫色

36 兰色

37 浅紫色

38 高亮原色

################################

 

################################

print显示字体背景颜色代码整理:

格式:

” XXX\033[41;1mYY\033[0m”

红色字体的31为表示字体的背景颜色的颜色代码,31表示输出的YY字体的背景颜色为红色

字体颜色代码整理如下:

39 字体高亮背景无色

40 高亮原色

41 红色

42 绿色

43 黄色

44 蓝色

45 紫色

46 深蓝色

47 灰色

48 无色

 

版权声明:本文为mypython2018原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mypython2018/p/02-phthon-shopping-cart.html