Lesson guide
What this Python exercise practices
Return Absolute Value is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Summary
Compute the absolute value of a number using conditional logic (no built-in abs()).
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
Explanation
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 input 0
abs_value(-3)
Sample output 0
3
Explanation 0
Absolute value of -3 is 3.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.