I came up with:
def ducks_and_dogs(heads=0, feet=0):
solutions = []
if feet != 0 and feet % 2 == 0:
solutions = [
(ducks, dogs)
for ducks in range(heads) for dogs in range(heads)
if ducks + dogs == heads and ducks * 2 + dogs * 4 == feet
]
if solutions:
for solution in solutions:
print('{} heads and {} feet could be from {} ducks and {} dogs'.
format(heads, feet, solution[0], solution[1]))
else:
print('{} heads and {} feet has zero solutions'.format(heads, feet))
else:
print('{} heads and {} feet should not happen'.format(heads, feet))
def main():
tests = [
{'heads': 45, 'feet': 122},
{'heads': 74, 'feet': 177},
{'heads': 12, 'feet': 8},
]
[ducks_and_dogs(**test) for test in tests]
main()
You should have enough there to write your own solution.