'Is there a way to break out from a inner for loop but not outer for loop in python
I am very new to python Please help. how can I break from a inner for loop in a nested for loop in python. I tried the 'break' statement but it is exiting from both for loops and I want to exit from just inner for loop. Actually I am trying to find subarray (of a given array) with certain sum.
def subArraySum(A,Sum):
curr_sum = A[0]
for i in range(len(A)):
for j in range(i+1,len(A)):
if curr_sum == Sum:
return [i,j]
if curr_sum>Sum or j==len(A):
break
curr_sum = curr_sum+A[j]
return [-1]
N = int(input())
Sum = int(input())
A = list(map(int,input().split()))
ans = subArraySum(A, Sum)
for i in ans:
print(i, end='')
print()
Solution 1:[1]
for i in range(5):
for j in range(5):
if j == 3:
break
print(i, j)
Output:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
3 0
3 1
3 2
4 0
4 1
4 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Parvesh Kumar |
