在列表中打印单个键+值,嵌套在字典中

时间:2020-02-28 09:41:15

标签: python list dictionary

我有以下内容:

device = {
    'BPCM' : ['phone', 'Description: Compact', 'price', 29.99],
    'BPSH' : ['phone', 'Description: Clam Shell', 'price', 49.99],
    'RTMS' : ['Tablet', 'Description: RoboTab - 10-inch screen and 64GB memory', 'price', 299.99],
    'RTLM' : ['Tablet', 'Description: RoboTab - 10-inch screen and 256 GB memory', 'price', 499.99],
} 

print("Hello customer, here is your list of phone and tablet choices. Please choose a phone or tablet by entering your name and the item code:")  
for key, value in device.items():
    print("\nItem Code: " + key)
    print(" " + str(value)) 
name = raw_input("\nPlease tell me your name? ") 
print("Hello " + name)
response = raw_input("Please enter item code: ") 
if response in device.keys():
    print("Item Code Chosen " + response + str(value))
else: 
    print("This code " + response + " is not in our list, please try again or choose quit 'q'") 

此行:“ print(”选择的项目代码“ +响应+ str(value))”似乎引起问题,因为它打印了以下输出:

Item Code Chosen BPCM['phone', 'Description: Clam Shell', 'price', 49.99]

上面打印的值是字典中的第二个值,与键:RTLM有关,但我希望第一个值如下:

'BPCM'['phone', 'Description: Compact', 'price', 29.99] 

我看不到我的错误?

1 个答案:

答案 0 :(得分:1)

您需要如下更改0

str(value)