product_list = [
(‘iphone6s‘,5800),
(‘kindle‘,800),
(‘tesla‘, 900000),
(‘python book‘, 105),
(‘bike‘,2000),
]
salary = input("Salary:")#输入工资
shopping_car = []
if salary.isdecimal():
salary=int(salary)
while True:
#打印商品内容
for i,v in enumerate(product_list,1):
print(i,‘>>>>‘,v)
#引导用户选择商品
choice = input("选择商品编号[退出:q]:")
#验证输入是否合法
if choice.isdigit():
choice = int(choice)
if choice > 0 and choice <= len(product_list):
#将用户选择商品通过choice取出来
p_iterm = product_list[choice-1]
#如果钱够,用本金salary减去该商品价格,并将该商品加入购物车
if p_iterm[1]<salary:
salary -= p_iterm[1]
shopping_car.append(p_iterm)
else:
print(‘余额不足,还剩%s‘%salary)
print(p_iterm)
else:
print(‘编码不存在‘)
elif choice == ‘q‘:
print(‘-----------您已购买如下商品---------‘)
#循环遍历购物车里面的商品,购物车存放的是已买商品
for i in shopping_car:
print(i)
print(‘您还剩%s元钱‘%salary)
break
else:
print(‘invalid input‘)