购物功能

 1 # 产品列表
 2 product_list = [
 3     ('红米note5', 1099),
 4     ('红米s2', 999),
 5     ('红米6', 799),
 6     ('红米6A', 599),
 7     ('小米8', 1799),
 8     ('小米MAX3', 1699),
 9     ('小米MIX2S', 3299),
10 ]
11 shop_list = []
12 
13 salary = input('请输入工资:')
14 if salary.isdigit():
15     salary = int(salary)
16     while True:
17         for index,item in enumerate(product_list):
18             print('商品编号:{} {}'.format(index, item))
19         user_choice = input('请输入您要购买的商品编号,退出请输入q')
20         if user_choice.isdigit():
21             user_choice = int(user_choice)
22             if 0 <= user_choice < len(product_list):
23                 p_item = product_list[user_choice]
24                 if salary >= p_item[1]:
25                     shop_list.append(p_item)
26                     salary -= p_item[1]
27                     print('已购买商品:{},余额:33[31;1m{}33[0m'.format(shop_list,salary))
28                 else:
29                     print('余额不足,剩余:33[31;1m{}33[0m'.format(salary))
30             else:
31                 print('暂无此产品,请重新选择')
32         elif user_choice == 'q':
33             print('-' * 10, '已购买商品', '-' * 10)
34             for p in shop_list:
35                 print(p)
36             print('余额:33[31;1m{}33[0m'.format(salary))
37             exit()
38         else:
39             print('...')
原文地址:https://www.cnblogs.com/gaowy/p/9687871.html