Exercise 1.10 - create an abs() function§

Let’s create a function that returns the absolute value of a numeric (a float or int). Actually Python includes one called abs(), but we’ll make our own called absol() for sake of the exercise.


1.10 a)§

Use an if statement that will reverse the sign of a variable x if x is negative (that’s all an abs() function does).


1.10 b)§

Insert your if statement into a function called absol so that it returns the correct value.


1.10 c)§

Test the output of your new function against the built-in abs() for 100 random numbers between -1 and 1. Count the number of fails and report with a print statement at the end whether the method works.

Go to Solution 1.10