[英]print single key+value within a list, nested within a 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("Item Code Chosen " + response + str(value))" 似乎导致了问题,因为它打印了以下输出:
Item Code Chosen BPCM['phone', 'Description: Clam Shell', 'price', 49.99]
上面打印的值是字典中的第二个值,与键相关:RTLM 但我希望第一个值如下:
'BPCM'['phone', 'Description: Compact', 'price', 29.99]
我看不到我的错误?
你需要改变str(value)
如下:
if response in device.keys():
print("Item Code Chosen " + response + str(device[response]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.