点击(此处)折叠或打开
-
#!/usr/bin/env python
-
# -*- UTF-8 -*-
-
# Author:ChengZhen
-
-
products_list=[['001','Nokia',1800],['002','三星',3000],['003','小米',1999],['004','华为',2999],['005','iphone',6999],['006','candle',800]]
-
shopping_cart=[]
-
-
salary=input("\033[31;1mPlease input your salary:\033[0m")
-
-
if salary.isdigit():
-
salary=int(salary)
-
while True:
-
for goods in enumerate(products_list):
-
print(goods)
-
product_id=input("\033[31;1m请输入要购买的商品编号:\033[0m")
-
if product_id.isdigit():
-
product_id=int(product_id)
-
if product_id<len(products_list) and product_id>=0:
-
if salary>=products_list[product_id][2]:
-
shopping_cart.append(products_list[product_id][1])
-
salary-=products_list[product_id][2]
-
print("\033[31;1m添加商品:%s到购物车,当前余额为:%s\033[0m"%(products_list[product_id][1],salary))
-
else:
-
print("\033[31;1m现在工资余额为:%s,不足以支付该商品:%s\033[0m"%(salary,products_list[product_id][1]))
-
-
else:
-
print("\033[31;1m输入的商品编号不存在,请重新输入\033[0m")
-
elif product_id=='q':
-
print("\033[31;1m购买的商品列表如下:%s,当前余额为:%s\033[0m"%(shopping_cart,salary))
-
exit()
-
else:
-
print("\033[31;1m商品编号输入错误\033[0m")
-
else:
- print("\033[31;1m输入的工资必须为数字\033[0m")