Mythic-ctrl
Mythic-ctrl

Reputation: 37

Why is my code returning False when the if statement criteria are met?

The goal is to return True if nums (a non-negative number) is within 2 of a multiple of 10.

The test cases that don't work are: near_ten(19) → True (my code returns False) near_ten(158) → True (my code returns False)

def near_ten(num):
  a = num % 10
  if a <= 2:
    return True
  elif a > 2:
    return False

Upvotes: 1

Views: 62

Answers (2)

bhristov
bhristov

Reputation: 3197

You were missing the check for 8 and 9 which are still within 2 of 10.

def near_ten(num):
  a = num % 10
  if a <= 2 or a >= 8: # we want to get 0 1 2 and 8 9
    return True
  return False # in all other cases it is False

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 114088

how bout

def close_to_ten(num):
    return not (2  < num%10 < 8)

Upvotes: 2

Related Questions