Reputation: 1
I have no clue how to clearly simplify the requirements for the if statement to run. Is there a simpler way I could have done this? The code is supposed to return True if the number is 2 integers near 10, either being above or below.
def nearten(num):
if (abs(num - 2) % 10) == 0 or (abs(num + 2) % 10) == 0 or (abs(num - 1) % 10) == 0 or (abs(num + 1) % 10) == 0 or num % 10 == 0:
return True
return False
Upvotes: 0
Views: 53
Reputation: 5470
You're certainly over-complicating things. Here is a much more flexible version:
def near_ten(num, close=2):
return abs(10 - num) <= close
An alternate version (if you're looking for numbers close to any multiple of 10):
def near_ten_multiple(num, close=2):
return abs(10 - (num % 10)) <= close
Upvotes: 3