购物功能

# 产品列表
product_list = [
('红米note5', 1099),
('红米s2', 999),
('红米6', 799),
('红米6A', 599),
('小米8', 1799),
('小米MAX3', 1699),
('小米MIX2S', 3299),
]
shop_list = []

salary = input('请输入工资:')
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print('商品编号:{} {}'.format(index, item))
user_choice = input('请输入您要购买的商品编号,退出请输入q')
if user_choice.isdigit():
user_choice = int(user_choice)
if 0 <= user_choice < len(product_list):
p_item = product_list[user_choice]
if salary >= p_item[1]:
shop_list.append(p_item)
salary -= p_item[1]
print('已购买商品:{},余额:\033[31;1m{}\033[0m'.format(shop_list,salary))
else:
print('余额不足,剩余:\033[31;1m{}\033[0m'.format(salary))
else:
print('暂无此产品,请重新选择')
elif user_choice == 'q':
print('-' * 10, '已购买商品', '-' * 10)
for p in shop_list:
print(p)
print('余额:\033[31;1m{}\033[0m'.format(salary))
exit()
else:
print('...')

猜你喜欢

转载自www.cnblogs.com/gaowy/p/9687871.html