Python-Programming-Lesson-Notes

7.4 Close Enough

Write some conditions that print True if the variable a is within 10% of the variable b and False otherwise. Compare your implementation with your partner’s: do you get the same answer for all possible pairs of numbers?

Hint

There is a built-in function abs that returns the absolute value of a number:

    print(abs(-12))
    12
Solution 1
a = 5
b = 5.1

if abs(a - b) <= 0.1 * abs(b):
    print('True')
else:
    print('False')
Solution 2
print(abs(a - b) <= 0.1 * abs(b))
This works because the Booleans ```True``` and ```False``` have string representations which can be printed.

Episode 7 exercise 5