0

From which loop is code exiting on using return True on line 5, just tell is it exiting from inner loop or outer loop

1 def check_sum(num_list): 2. for n1 in num_list: 3. for n2 in num_list: 4. if n1 + n2 == 0: 5. return True 6. return False 7. num_list = [10,-14,26,5,-3,13,-5] 8. print(check_sum(num_list)) output = True

24th Dec 2019, 2:31 PM
amith
2 odpowiedzi
+ 2
If you use return in a user function, the function will be terminated. It does not matter if this happens in an outer or in an inner loop. (In your case it is the inner loop). The condition in line 4 will be True when n1 is 5 and n2 is -5.
24th Dec 2019, 4:39 PM
Lothar
Lothar - avatar
+ 1
when n1 + n2 == 0 both loops stop and the function exits with return True.
24th Dec 2019, 3:15 PM
Bahhaⵣ
Bahhaⵣ - avatar