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.
Use an if statement that will reverse the sign of a variable x if x is negative (that’s all an abs() function does).
Insert your if statement into a function called absol
so that it returns the correct value.
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