How to count horseshoes in python?

Question:

I’ve completely stuck with this task and I really dunno how to make this program work properly, because I think I’ve already tried many possible options, but it still unfortunately didn’t work properly.

The task is: "The blacksmith has to shoe several horses and needs to see if he has the correct number of horseshoes. Write a check(p, k) function that, for a given number of horseshoes p and number of horses k, prints out how many horseshoes are missing, remaining, or whether the number is correct (see sample file for output format)."

The code I’ve already done is:

def check(p, k):
    if p % 2 == 0 and k % 2 == 0 and p % k == 0:
        print("Remaining:", k % p)     
    elif p % k != 0:
        print("Missing:", p // k + 1)
    else:
        print("OK")

check(20, 6)
check(10, 2)
check(12, 3)
check(13, 3)

The output should look like this:

Missing: 4
Remaining: 2
OK
Remaining: 1

Answers:

Try checking if you have the correct number first:

def check(p, k):
    required_shoes = k * 4
    if p == required_shoes:
        # just right
    elif p < required_shoes:
        # not enough
    else:
        # too many
Answered By: jprebys
def check(p, k):
    if p / k == 4:
        print("OK")
    elif p / k > 4:
        print("Remaining:", p - 4 * k)
    else:
        print("Missing:", 4 * k - p)

check(20, 6) # Missing: 4
check(10, 2) # Remaining: 2
check(12, 3) # OK
check(13, 3) # Remaining: 1

This works in the given cases

Answered By: Thicc_Gandhi

You could try this:

def check(shoes, horses):
  if shoes > 4*horses:
     print("Remaining:", shoes - 4 * horses)
  elif shoes == 4*horses:
     print("OK")
  else:
     print("Missing:", 4 * horses - shoes)
Answered By: Progg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.