The absolute difference is a measure of the difference between two numbers but expressed as a positive number.
In other words, it’s taking the difference between two values (x - y) and then calculating the absolute value of the result.
Create a function (called as absDiff in the examples below; use the name from the starter code) that takes in two numbers a and b and returns their absolute difference.
Examples:
absDiff(3, 0) ➞ 3 absDiff(0, 3) ➞ 3 absDiff(10, -1) ➞ 11 absDiff(-5, -7) ➞ 2 absDiff(6, -6) ➞ 12 absDiff(7, 7) ➞ 0
Note:
Do not use the absolute value function provided by Python (it will defeat the purpose of the challenge)
Hints:
If
a is less than b, the absolute difference equals b - a. What if a is greater than or equal to b?
If
a < b, we should return b - a. Otherwise, we should return a - b. We can use an if statement to put this in code.
Free plan: 30 tutor messages. PRO is unlimited.
