我已经完全坚持这个任务,我真的不知道如何使这个程序正常工作,因为我认为我已经尝试了许多可能的选择,但不幸的是它仍然不能正常工作。
任务是:“铁匠必须给几匹马穿鞋,并且需要看他是否有正确的马蹄铁数量。写一个检查(p,k)函数,对于给定数量的马蹄铁p和马数k,打印出丢失了多少马蹄铁,剩余了多少马蹄铁,或者这个数字是否正确(输出格式见示例文件)。”
我已经做过的代码是:
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)输出应该如下所示:
Missing: 4
Remaining: 2
OK
Remaining: 1发布于 2022-10-31 20:21:38
你可以试试这个:
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)发布于 2022-10-31 20:19:07
试着先检查一下是否有正确的号码:
def check(p, k):
required_shoes = k * 4
if p == required_shoes:
# just right
elif p < required_shoes:
# not enough
else:
# too many发布于 2022-10-31 20:21:29
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这在给定的情况下是可行的。
https://stackoverflow.com/questions/74268637
复制相似问题