Home > front end >  Breaking the inner loop while continuing the outer loop
Breaking the inner loop while continuing the outer loop

Time:11-06

I’m working through problem 23 on Project Euler and I’m attempting to break the innermost loop while proceeding with the outer loop. I found that when the inner loop first breaks, it’s both loops that break. Is there a way that I can terminate the inner loop early and return to the outer loop?

`

for i in range (0,len(abundant)):
  for j in range (0,len(abundant)):
      sum = abundant[i]   abundant[j]
      try:
        canBeTheSum[sum] = 0
      except:
        break

`

CodePudding user response:

Consider this instead. You don't really need the indexes, you just need the numbers.

for i in abundant:
  for j in abundant:
    s = i j
    if s >= len(canBeTheSum):
      break
    canBeTheSum[s] = 0

CodePudding user response:

Update: I realised the issue with my code was an error in the logic of another for loop elsewhere.

  • Related