给出以下python代码:
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],
}
sims = {
"SMNO" : ['SIM card', 'Description: SIM Free (no SIM card purchased)', 'price', 0.00],
"SMPG" : ['SIM card', 'Description: Pay As You Go (SIM card purchased)', 'price', 9.99],
}
print("Hello customer, here is your list of phone, tablet and SIM choices choices. Please choose a phone or tablet by entering your name and the item code:")
for key, value in device.items() + sims.items():
print("\nItem Code: " + key)
print(" " + str(value))
name = raw_input("\nPlease tell me your name? ")
print("Hello " + name)
device_choice = raw_input("Please enter item code for the phone or tablet you want?: ")
if device_choice in device.keys():
print("Item Code Chosen " + device_choice + str(device[device_choice]))
if str(device[device_choice]) == 'Phone':
print("\nSIMS available: " + str(sims[keys]))
print("\nPlease tell me if you want SIM free or Pay As You Go by choosing its item code? ")
上面的这些行似乎有问题:
if str(device[device_choice]) == 'Phone':
print("\nSIMS available: " + str(sims[keys]))
是否不会像匹配“电话”字符串那样打印sims词典的键和值?任何帮助表示赞赏...
答案 0 :(得分:0)
第str(device[device_choice])
行引用了字典中的列表值。
例如。
print(str(device['BPCM']))
output:
['Phone', 'Description: Compact', 'price', 29.99]
我想你想做的是
if str(device[device_choice][0]) == 'Phone':
print("\nSIMS available: " + str(sims[keys]))
如果产品是电话,则应该通过if条件。 希望这会有所帮助!