Sunner
Sunner

Reputation: 33

python program incorrect output about function

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]

I want to calcuate a number of numPigs and numChicks. I input slove(20,56), but the answer is None None. Could you tell me what's wrong with my program?

Upvotes: 1

Views: 36

Answers (1)

Jay Parikh
Jay Parikh

Reputation: 2489

This should work !


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]

Upvotes: 1

Related Questions