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),但答案是“无”。
你能告诉我我的程序怎么了吗?

最佳答案

这应该工作!


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]

关于python - python程序关于功能的错误输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45232822/

10-12 22:45