dct = {1:'James', 2:'Alex', 3:'Thomas'}
if dct == 'James':
print('There is a James in the dictionary.')
else:
print("There is no James.")
我似乎无法弄清楚为什么这个if-else语句不起作用,我知道它有点小但是我一直得到错误的输出。谁知道为什么?这是我的字典有问题吗?谢谢你的帮助!
答案 0 :(得分:2)
你需要这样的东西:
dct = {1:'James', 2:'Alex', 3:'Thomas'}
if 'James' in dct.values():
print('There is a James in the dictionary.')
else:
print("There is no James.")
in
运算符来比较'James'
.values()
和使用in
的列表是最简单和最简洁的方式。