How can I make the not_sum_to_ten() function ignore negative numbers?

Question

For this exercise, the not_sum_to_ten() function will return True for the numbers 9 and -1 and return False for 9 and 1. If I wanted to make the function ignore whether the numbers were negative, how would that work?

Answer

The Python abs() function will return the absolute value of a number. Calling the abs() function during the check as shown below will cause the function to treat 9 and -1 the same as 9 and 1.

if (abs(num1) + abs(num2) != 10):
10 Likes

I loved the question and the answer here… I am loving this a little more every day!

6 Likes