如何检查Python字典中是否存在定义和键

我想检查两个用户的输入是否一起作为键值对一起存在于字典中;例如:

dict = {"ABC" : 123,"DEF" : 456}
firstInput= input("Please enter your age: ")
secondInput= input("Please enter your name: ")

然后: 如果用户输入的分别是“ ABC”和123,或者“ DEF”和456,它将返回一些值,如果没有,则返回另一个值。

g109007 回答:如何检查Python字典中是否存在定义和键

我很想不提供代码示例,因为似乎可以通过简单的Google搜索来回答此问题。话虽这么说:

try:
    value = dict[firstInput]
except KeyError:
    value = 'No entry for {}'.format(firstInput)

if value == secondInput:
    # success 
else:
    return 'Entry found but incorrect value supplied'
本文链接:https://www.f2er.com/3152199.html

大家都在问