【发布时间】:2021-04-11 01:32:06
【问题描述】:
fruit={'apple':'one','banana':'two','orange':'three'}
a='one'
if a in fruit.value():
print(a,"was found!")
当我在 python 上运行它时,它显示: AttributeError: 'dict' 对象没有属性 'value'
【问题讨论】:
标签: python
fruit={'apple':'one','banana':'two','orange':'three'}
a='one'
if a in fruit.value():
print(a,"was found!")
当我在 python 上运行它时,它显示: AttributeError: 'dict' 对象没有属性 'value'
【问题讨论】:
标签: python
字典没有称为 key() 的元素。您打算使用的是以 s 结尾的 keys() 。它应该是这样的:
fruit={'apple':'one','banana':'two','orange':'three'}
a='apple'
if a in fruit.keys():
print(f"Yes, key: '{a}' exists in dictionary")
【讨论】:
只需像这样检查它:
fruit={'apple':'one','banana':'two','orange':'three'}
a='apple'
if a in fruit:
print(a,"was found!")
【讨论】: