codingbat 问题:close_far |一次考试不及格 |帮助

codingbat Problem: close_far | failing only one test | heeeelp

练习: 给定三个整数 a b c,return 如果 b 或 c 中的一个是“close”(与 a 最多相差 1),而另一个是“far”,与其他两个值相差 2 或更多,则为真。注意:abs(num) 计算数字的绝对值。

https://codingbat.com/prob/p160533

我的代码:

def close_far(a, b, c):
  if (b == a + 1 or a - 1 or a) or (c == a + 1 or a - 1 or a):        #looking for the "close" one
    if (c > a + 2 and b + 2) or (c <= a - 2 and b - 2):               #looking for c to be the "far" one
        return True
    elif (b > (a + 2 and c + 2)) or (b <= (a - 2 and c - 2)):         #looking for b to be the "far" one
        return True
    else:    
      return False

错误答案 -> close_far(4, 3, 5) → False True X

我的代码给出了 True,尽管它是 False。


我实际上不知道我在那里做错了什么。我想我的第二个 if 语句有问题......括号? or/and ? 感谢任何帮助!

screenshot

嗯,我没看错!我弄乱了 elif 语句中的括号,导致测试失败。约翰尼莫普,你让我走上了正确的道路,谢谢你的帮助。

def close_far(a, b, c):
  if (b == a + 1 or a - 1 or a) or (c == a + 1 or a - 1 or a):              #looking for the "close" one
    if ((c > a + 2) and (c > b + 2)) or ((c <= a - 2) and (c <= b - 2)):    #looking for c to be the "far" one
        return True
    elif (b > (a + 2 and c + 2)) or ((b <= a - 2) and (b <= c - 2)):        #looking for b to be the "far" one
        return True
    else:    
      return False

尝试了简单的 OR 和 AND 操作。对我来说很好。

def close_far(a, b, c):
  if abs(b-c)>=2:
    if (abs(a-b)<=1 and abs(a-c)>=2) or ( abs(a-c)<=1 and abs(a-b)>=2):
      return True
  return False