1 shopping_list = [] 2 while True: 3 print("=========商品选项==============") 4 msg_dic = { 5 ‘apple‘: 10, 6 ‘tesla‘: 100000, 7 ‘mac‘: 3000, 8 ‘lenovo‘: 30000, 9 ‘chicken‘: 10, 10 } 11 for k,v in msg_dic.items(): 12 # \033[44m.... \033[0m表示字体的背景颜色 13 print(‘name:\033[44m{name} \033[0m price:\033[42m {price}\033[0m‘.format(price=v,name=k)) 14 print("==============================") 15 shname=input("请输入以上你需要的商品的名称:>>").strip() 16 if len(shname)==0 or shname not in msg_dic: 17 continue 18 while True: 19 number = input("请输入你的购买个数:>>").strip() 20 if number.isdigit(): 21 break 22 # 将商品名,价格,购买个数加入购物列表 23 shopping_list.append((shname,msg_dic[shname],int(number))) 24 print(shopping_list)
原文:https://www.cnblogs.com/bashliuhe/p/12442636.html