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 successfully.
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
CodePudding user response:
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)
CodePudding user response:
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
CodePudding user response:
def check(p, k):
if p / k == 4:
print("OK")
elif p / k > 4:
print("Remaining:", p % 4)
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