import traceback goods = [ {‘name’:‘衣服’ ,‘price’:200}, {‘name’:‘猫粮’ ,‘price’:500}, {‘name’:‘衣架’ ,‘price’:20}, {‘name’:‘猫砂’ ,‘price’:50}, {‘name’:‘可乐’ ,‘price’:3}, ]
shoping = [] shopingmoney = 0
while True: try: money = int(input(‘请输入你的余额:’)) except: print(‘输入错误,请重试!’) traceback.print_exc(file=open(‘question.txt’,‘a’)) else: break
print(‘我们有以下商品:’) for i in goods: print(i[‘name’],i[‘price’])
while True: try: want = input(‘请输入您想购买的商品: 输入y将进行结算:’) if want == ‘y’: break for i in goods: if want in i[‘name’]: shoping.append(i) break else: raise ValueError
except ValueError:
print('您想购买的产品我们没有哦,请重新输入!')
traceback.print_exc(file=open('question.txt','a'))
print(‘您购买的商品为:’, shoping)
for i in shoping: shopingmoney += i[‘price’]
print(‘结算成功您剩余的钱为:’, money - shopingmoney)
仅供参考
|