goods =
[
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
while True:
for value in goods:
print(goods.index(value)+1,value[‘name‘],value[‘price‘]) # 索引值+1,‘name键的值,value键的值
str_input = input(‘请输入你选择的序号,按Q或q退出:‘) # 输入内容是字符串
if str_input.isdigit() and 0 < int(str_input) < len(goods): # 如果是输入内容是数字而且在0和4之间
print(goods[int(str_input)-1][‘name‘],goods[int(str_input)-1][‘price‘]) # 商品序号对应数值-1的键对应的值
elif str_input.strip().upper() == ‘Q‘: # 如果去掉输入内容的空格并大写之后是"Q"
break # 跳出循环,退出程序
else:
print(‘输入有误,请重新输入!‘)
原文:https://www.cnblogs.com/ayongxin93/p/11000450.html