简体   繁体   中英

Python - Finding the difference between 2 numbers

So I'm trying to find a way to find the difference between 3 ints; a, b, c so I need to find the difference between a - b and the difference between b - c and then print true if the difference is the same, else printing false. I've been attempting to use the abs() function but can't seem to get the result I need. any help would be appreciated.

Here is the problem statement:

Given three ints, abc, one of them is small, one is medium and
one is large. Print True if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.

this is what i have so far;

a = int(input())
b = int(input())
c = int(input())

if abs(a-b) == abs(b-c) :
    print("True")
else :
    print("False")

#

Test Input  Expected Actual 
1     4 6 2    True  False
2     6 2 4    True  False
3   10 9 11    True  False

Based on the problem description, it sounds like you need to sort the numbers first:

numbers = sorted(int(input()) for _ in range(3))
print(numbers[1] - numbers[0] == numbers[2] - numbers[1])

You have 3 possible "middle" numbers, therefore you need to perform 3 comparisons.

x = abs(a - b)
y = abs(a - c)
z = abs(b - c)
if (x == y) or (x == z) or (y == z):
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM