python 遍历字典,输入相应的商品序号,打印相应的商品信息

img

goods=[{'name':'台式电脑','price':4999},
       {'name':'鼠标','price':10},
       {'name':'笔记本','price':6000},
       {"name":"投影仪","price":9999}]
for i,d in enumerate(goods):
      n=d["name"]
      m=d["price"]
      print(i+1,n,m)
while True:
    n=input('请输入商品序号:')
    if n in ['q','Q']:
        break
    r = [str(i) for i in range(1,len(goods)+1)]
    if n in r:
        good = goods[int(n)-1]
        print(good['name'],good['price'])
    else:
        print('输入有误,请重新输入')