python 购物小程序

#货物清单
product_list = [('Iphone',5800),
('Mac Pro',12000),
('coffee',31),
('Book',120),
('Bycle',800)]
#购物车清单
shopping_list = []
#顾客输入自己的工资
salary = input("请输入您的工资:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
usr_choice = input("请选择你要商品:")
if usr_choice.isdigit():
usr_choice = int(usr_choice)
if usr_choice < len(product_list) and usr_choice>=0:
p_item = product_list[usr_choice]
print(p_item)
if salary >= p_item[1]: #可以购买
shopping_list.append(p_item)
salary-=p_item[1]
print("添加 %s 到你的购物车,你现在余额还剩 \033[31;1m%s\033[0m" %(p_item,salary))
else:
print("\033[41;1m您的余额不足!\033[0m")
else:
print("\033[33;1m您选的商品不存在!\033[0m")
elif usr_choice =='Q':
print("------打印购物清单-----")
for i in shopping_list:
print(i)
print("你的余额还剩:\033[31;1m%s\033[0m"%(salary))
exit()
else:
print("\033[31;1m 你输入无效!\033[0m")
上一篇:python 协程中的手动切换Greenlet和自动Gevent


下一篇:Mac小技巧:利用终端轻松制作出下雪效果