tiger1234522
tiger1234522

Reputation: 27

How to make 1 variable minus the other variable always positive?

Hi so i want to make 1 variable minus the other variable then see if the number will be less then a certain number. However sometimes when one variable minus the other one, it will produce a negative number which will always be less then the number. How do i make it such that the variables minusing each other will give a positive number always

a = int(input("Input a integer")
b = int(input("Input a integer")
if a - b < 3 :
print(a-b is close to equaling 0)

Upvotes: 0

Views: 434

Answers (2)

12ksins
12ksins

Reputation: 307

Use the abs function. It's like |a - b| in math. Or in other words, it returns the absolute value—will make the number positive.

if abs(a - b) < 3:
    # code

Upvotes: 1

Tabaene Haque
Tabaene Haque

Reputation: 574

May be you are looking for something like this -

a = int(input("Input a integer"))
b = int(input("Input a integer"))
if abs(a - b) < 3 :
    print("a-b is close to equaling 0")

abs function turns your result into positive. There is also indentation and syntax error in your print statement, " is missing. Also closing ) is missing on line 1 and 2.

Upvotes: 3

Related Questions