easy +5 pts

Absolute Difference

Compute the absolute difference between two numbers with a simple function.

Write a function `absolute_difference(x, y)` that takes two numbers (integers or floats) and returns the absolute difference between them. That is, the non-negative result of subtracting the smaller from the larger. Do not use any built-in abs() function; compute it manually. The function should return a number (int if both inputs are int, float if any input is float).

Constraints

Inputs are integers or floats. The result will fit within standard Python numeric precision. Do not use abs().

Example

>>> absolute_difference(10, 4)
6
>>> absolute_difference(3.5, 1.0)
2.5
>>> absolute_difference(-7, -2)
5
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

If x is greater than y, the difference is x - y; otherwise it is y - x.
You can use a conditional expression (ternary) to choose the order of subtraction.
Consider both integers and floats — the manual method works for both.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.