Day4_python(购物车代码)

# Welcome Python
sale=0#消费金额
product_list=[
('苹果',100),
('红苹果',200),
('青苹果',150),
('香蕉',100),
('小番茄',80),
]
#商品列表
shop_car = []
#购物车
salary = input("你带了多少钱:")
if salary.isdigit():
salary = int (salary)
salary1 = salary
while True:

#for item in product_list:
for index,item in enumerate(product_list):
#print(product_list.index(item),item)
print(index,item)
user_choice=input("选择要买的水果【q】:")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice < len(product_list) and user_choice>=0:
p_item=product_list[user_choice]
if p_item[1]<= salary:
shop_car.append(p_item)
salary-=p_item[1]
sale+=p_item[1]
print("添加 %s 进购物车,你的余额为 \033[31;1m%s\033[0m"%(p_item,salary))
else:
print("\033[41;1m余额不足,剩余[%s]\033[0m"%salary)
else:
print("选择的[%s]商品不存在!"%user_choice)
elif user_choice == 'q':
print("------已购买商品-------")
for p in shop_car:
print(p)
print("您一共需要支付",sale,'元',"收您",salary1,"元","找您:",salary,'元')

exit()
else:
print("请输入商品序号")
else:
print("请正确输入工资")
posted @ 2017-03-17 01:18  胡飞侠5  阅读(265)  评论(0编辑  收藏  举报