Editor

Absolute Value

Python Basics · Beginner · 35 XP · about 4 min

Write a function called absolute(n) that returns the absolute value of a number (without using Python's built-in abs() function).

The absolute value of a number is its distance from zero, always positive.

Example

absolute(5)    # Returns 5
absolute(-5)   # Returns 5
absolute(0)    # Returns 0
absolute(-100) # Returns 100

Requirements

  • Do NOT use the built-in abs() function
  • Return the absolute value as a number
  • Handle both positive and negative inputs
def absolute(n):
# Your code here - don't use abs()!
pass
# Test your function
print(absolute(-5))
print(absolute(5))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.