关于函数的python程序输出错误

时间:2017-07-21 08:30:53

标签: python

def slove(numLegs, numHeads):
    for numChicks in range(0, numHeads + 1):
        numPigs = numHeads - numChicks
        totLegs = 4*numPigs + 2*numChicks
        if totLegs == numLegs:
            return [numPigs, numChicks]
        return [None, None]

我想计算一些numPigs和numChicks。 我输入了slove(20,56),但答案是None None。 你能告诉我我的课程有什么问题吗?

1 个答案:

答案 0 :(得分:1)

这应该有效!


def slove(numLegs, numHeads):
    for numChicks in range(0, numHeads + 1):
        numPigs = numHeads - numChicks
        totLegs = 4*numPigs + 2*numChicks
        if totLegs == numLegs:
            return [numPigs, numChicks]
    return [None, None]