''' 功能: 1.循环输出商品列表 2.显示账户余额,判断能否买的起 3.选择相应商品的数字,将其加入购物车 4.输出购买的东西 随时能够退出 ''' dic = { 1:['书本',10], 2:['篮球',50], 3:['足球',100], 4:['车子',10000], 5:['房子',100000] } shop = [] for k,v in dic.items(): print('{}\t\t{}\t\t{}'.format(k,v[0],v[1])) judgement = 1 balance = int(input('请输入你的预算')) s = input('请输入你想要的在1-{}范围内的商品序列号'.format(len(dic))) ss = 0 sss = 0 while judgement: if s.strip().isdigit(): if int(s) <= len(dic) and int(s) > 0: ss = int(balance) // dic[int(s)][1] count = input('你可以买{}个,请输入你想买的个数'.format(ss)) sss = dic[int(s)][1] * int(count) while not (sss <= int(balance) and sss > 0): count = input('请重新输入购买个数,最多{}个'.format(ss)) sss = dic[int(s)][1] continue else: print('你购买了{0}个{1},共花去{2},还剩{3}'.format(count,dic[int(s)][0],ss,int(balance) - ss)) shop.append('{}*{}'.format(dic[int(s)][0], count)) s = input('是否继续购买,继续购买请继续输入在1-{}范围内的序列号,若想退出,输入q/Q'.format(len(dic))) continue else: s = input('请输入1-{}范围内的数字'.format(len(dic))) continue elif s.isalpha(): if s.strip().upper() == 'Q': # shop.append('{}*{}'.format(dic[s][0],count)) judgement = 0 else: s = input('请输入1-{}范围内的数字'.format(len(dic))) continue else: s = input('请输入1-{}范围内的数字'.format(len(dic))) continue print(shop)