если условное совпадение значения ключа ключа python словарей не печатается - PullRequest
0 голосов
/ 28 февраля 2020

Учитывая приведенный ниже код 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 как Я ожидаю, основываясь на сопоставлении строки «Телефон»? Любая помощь высоко ценится ...

1 Ответ

0 голосов
/ 28 февраля 2020

Строка 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, если продукт является телефоном. Надеюсь, это поможет!

...