使用Python字典值作为结果返回键

使用Python字典值作为结果返回键,python,dictionary,Python,Dictionary,我不知道为什么这段代码在一些输入中失败(从CodingBat,链接到问题:)。下面是问题的详细信息,我可能可以使用if elif语句来回答这个问题,但我想使用字典。此外,我还了解到,不建议像下面使用的那样从字典中获取键值。但如果能指出以下节目中的问题,我将不胜感激 你开得有点太快了,一个警察拦住了你。编写代码计算结果,编码为int值:0=无票,1=小票,2=大票。如果速度小于等于60,则结果为0。如果速度介于61和80之间,则结果为1。如果速度大于等于81,则结果为2。除非是你的生日——在那天,

我不知道为什么这段代码在一些输入中失败(从CodingBat,链接到问题:)。下面是问题的详细信息,我可能可以使用
if elif
语句来回答这个问题,但我想使用字典。此外,我还了解到,不建议像下面使用的那样从字典中获取键值。但如果能指出以下节目中的问题,我将不胜感激

你开得有点太快了,一个警察拦住了你。编写代码计算结果,编码为int值:0=无票,1=小票,2=大票。如果速度小于等于60,则结果为0。如果速度介于61和80之间,则结果为1。如果速度大于等于81,则结果为2。除非是你的生日——在那天,你的速度在任何情况下都可以提高5

  • 超速被抓(60,假)→ 0
  • 超速驾驶被捕(65,假)→ 一,
  • 超速驾驶被抓获(65,正确)→ 0
为……工作

caught_speeding(70, False)
caught_speeding(75, False)
caught_speeding(75, True)
caught_speeding(40, False)
caught_speeding(40, True)
caught_speeding(90, False)
caught_speeding(60, False)
caught_speeding(80, False)

看起来您混合了
Bir_dict
NoBir_dict
。你能试试下面的代码吗

def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        def getKey(dict,value):
            return [key for key in dict.keys() if (dict[key] == value)]
        if is_birthday:
            out1=getKey(Bir_dict,True)
            return out1[0]
        else:
            out2=getKey(NoBir_dict,True)
            return out2[0]

难道这个问题不会考虑生日字典有更高的可用值吗?< /P> 如果是我的生日,我要65岁了: 我希望没有票。 但是,如果生成词典并打印它们,您可以看到情况并非如此:

def caught_speeding(speed, is_birthday):
    Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
    print Bir_dict
    NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
    print NoBir_dict
    def getKey(dict,value):
        return [key for key in dict.keys() if (dict[key] == value)]
    if is_birthday:
        out1=getKey(Bir_dict,True)
        return out1[0]
    else:
        out2=getKey(NoBir_dict,True)
        return out2[0]

您只需将字典对象中的值交叉即可

Baa!非常感谢。我应该休息一下。感谢你的改进。
def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        def getKey(dict,value):
            return [key for key in dict.keys() if (dict[key] == value)]
        if is_birthday:
            out1=getKey(Bir_dict,True)
            return out1[0]
        else:
            out2=getKey(NoBir_dict,True)
            return out2[0]
def caught_speeding(speed, is_birthday):
        Bir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
        NoBir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
        def getKey(dict):
            return [key for key in dict.keys() if (dict[key] == True)]
        if is_birthday:
            out1=getKey(Bir_dict)
            return out1[0]
        else:
            out2=getKey(NoBir_dict)
            return out2[0]
def caught_speeding(speed, is_birthday):
    Bir_dict = {0:speed<=60,1:61<=speed<=80,2:speed>=81}
    print Bir_dict
    NoBir_dict = {0:speed<=65,1:66<=speed<=85,2:speed>=86}
    print NoBir_dict
    def getKey(dict,value):
        return [key for key in dict.keys() if (dict[key] == value)]
    if is_birthday:
        out1=getKey(Bir_dict,True)
        return out1[0]
    else:
        out2=getKey(NoBir_dict,True)
        return out2[0]
{0: False, 1: True, 2: False}
{0: True, 1: False, 2: False}
0
{0: False, 1: True, 2: False}
{0: True, 1: False, 2: False}
1