Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Return Absolute Value

Compute the absolute value of a number using conditional logic (no built-in abs()).

Python practice10 minControl Flow (If–Else Logic)BeginnerLast updated March 15, 2026

Problem statement

Write a function abs_value(x) that returns the absolute value of x. Do not use the built-in abs() function; instead use if/else to handle negative values and zero.

Task

Implement abs_value(x) that returns the non-negative magnitude of x.

Examples

Positive input

Input

abs_value(7)

Output

7

7 is already non-negative, so return 7.

Input format

A single number x passed as a function argument: abs_value(x).

Output format

Return the absolute value of x (int or float).

Constraints

x is an int or float. Avoid using built-in abs().

Samples

Sample 1

Input

abs_value(-3)

Output

3

Absolute value of -3 is 3.