0

We haven't exitted from the outer loop, then how we got result....

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, 1:48 PM
amith
4 Answers
+ 1
Add double dots after 0. Then it will not give any error. if n1 + n2 == 0:
24th Dec 2019, 1:57 PM
molang
molang - avatar
+ 1
One typical pattern to break out of a double loop is this: flag = False for... for... if break_condition: flag = True break if flag: break
24th Dec 2019, 2:18 PM
HonFu
HonFu - avatar
+ 1
amith if inner loop results in True the line 6 does not get executed because the function exits with return True. line 6 gets executed when both loop end with False.
24th Dec 2019, 3:05 PM
Bahha┣
Bahha┣ - avatar
0
@molang code exitted from inner loop when n1 + n2 becomes zero.... but code haven't exitted from outer loop..... no statement is provided for exitting from outer loop... then how line 6 get executed
24th Dec 2019, 2:04 PM
amith