-1

How to find if a number is less than or greater than 10 for difference.

Example1: If a is 100 and b is 91. This is almost Matching.

Example2: If a is 100 and b is 89. This is not at all Matching.

Below is the code and its working fine. Is there any other easiest or best way to achieve

a = 110
b = 100
c = a - b
d = a - 10
if a > b:
    if (a - b) <= 10:
        print "This is almost Matching"
    else:
        print "This is not at Matching"
else:
    if (b - a) <= 10:
        print "This is almost Matching"
    else:
        print "This is not at Matching"

Expected and actual are getting same

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
sagar_dev
  • 83
  • 2
  • 10

2 Answers2

2

You need to look for the absolute value of the difference (a,b):

The method abs() returns absolute value of x - the (positive) distance between x and zero.

a = 100
b = 110
print(abs(a - b))  # 10

if abs(a -b) <= 10:
    print("This is almost Matching")
else:
    print("This is not at Matching")

OUTPUT:

10
This is almost Matching
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

Use abs to find the absolute difference:

a = 110
b = 100
c = abs(a-b)
if c<=10:
    print ("This is almost Matching")
else:
    print ("This is not at Matching")