python - 如果没有打印 python 字典键字符串值的条件匹配
问题描述
给定下面的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]))
是不是像我期望的那样基于匹配“电话”字符串打印模拟字典的键和值?非常感谢任何帮助...
解决方案
该行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 条件。希望这可以帮助!
推荐阅读
- angular - Angular nx-daterangepicker-material 获取日期
- python - pandas 是否读取完整的数据文件并将其存储在数据框中?在 pandas 中加载 100mb 文件是否有效?
- java - How to show global header(array type) parameter in Swagger UI
- php - Update woocommerce cart dynamically
- sql-server - How come selecting date range is slower than comparing a date column with cast on it
- python - How to only remove the numbers that are before a string and not after? (inside data frame)
- javascript - 更新按钮文本后,引导图标丢失
- javascript - 如何从另一个页面重定向到某个 window.location.hash
- android - How to fix "ERROR: Could not get unknown property 'assembleRelease' for FileBasedMavenArtifact_Decorated aar"
- python - python3.6用unicode创建win32快捷方式