product_list = [
('IPhone',5900),
('Mac pro',9800),
('Bike',800),
('Watch',16000),
('Coffee',35),
('Tea',88)
]
shopping_list = []
sum1 = 0
print (product_list)
salary = input("输入你的金额:")
if salary.isdigit():
salary = int(salary)
while True:
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: #判断余额是否够
shopping_list.append(p_item)
salary -= p_item[1]
sum1 +=p_item[1]
print ("成功添加 %s 到购物车,你的当前余额是%s元:" % (shopping_list,salary))
else:
print("你的余额只剩%s元啦,快去充值吧!"% salary)
else:
print("你输入的产品编号%s不存在!"% user_choice)
elif user_choice == 'q':
print ("----shopping list------")
for p in shopping_list:
print(p)
print("购买成功!你的购买总额为:%s元,当前余额是:%s元" % (sum1,salary))
exit()
else:
print ("你输入无效的商品编号,请重新输入!")
continue
else:
print ("请输入正确的金额!")