1 product_list = [
2 ('IphoneX',5800),
3 ('Mar Pro', 9800),
4 ('Bike', 800),
5 ('Watch', 10600),
6 ('Coffee', 31),
7 ('Carbin Python',120),
8 ]
9 shopping_list = [] #定义一个空的购物车
10 salary = input("请输入你的工资:") #等待用户输入他的工资
11 if salary.isdigit(): #判断用户输入的是否为整数
12 salary = int(salary)
13 While True:
14 for index,item in enumerate(product_list): #enumennrate是获取元素的下标。
15 print(index,item)
16 user_choice = input("请输入你需要购买的商品序号:")
17 if user_choice.isdigit():
18 user_choice=int(user_choice)
19 if user_choice<len(product_list) and user_>=0: #len动态的获取列表的长度
20 p_item = product_list[user_choice]
21 shopping_list.append(p_item) #如果用户选择的商品在购物车里面,然后shopping_list就加入该商品
22 salary -=p_item[1] #减去你买的商品的价格
23 print("已经把%s加入你的购物车,你的余额剩下:\033[5;31;2M%s\033[0m "%(p_item,salary))
24 else:
25 print("对不起,你的余额还剩下:\033[5;31;2m%s\033 ,不能支付你所购买的商品 ") #\033[5;31;2m \033 这是字体的颜色,可以去百度查。
26 elif: user_choice =='exit'
27 print("!-------------------Your shopping_list---------------!")
28 for p in shopping_list:
29 print(p)
30 print("你的余额还有:\033[5;31;2m",salary)
31 exit()
32 else:
33 print("大哥你的工资不足以支付此商品,请继续努力")