Last answered:

08 Jun 2023

Posted on:

08 Jun 2023

0

Purpose of "return -1"

Hello,


I am running Professor Giles's solution. I am not sure what the purpose is of "return -1". If I remove "return -1" or change -1 to some other number, the function still runs and the solutions shown for the following are still the same. 


L = [8,6,11,3]
print(two_sum(L,9))


L2 = [2,5,3,7,4]
print(two_sum(L2,10))


print(two_sum([3, 3], 6))



1 answers ( 0 marked as helpful)
Instructor
Posted on:

08 Jun 2023

0

Hey Abbie,


Thank you for reaching out!


The function returns -1 if it doesn't find a pair of numbers that sum up to the desired result. For example, the input


L = [8,6,11,3]

print(two_sum(L,9))

would return -1. The choice of "-1" is arbitrary but often used in programming when you expect your function to return a positive number.


Hope this helps!


Kind regards,

365 Hristina 

Submit an answer