How can we exit a loop?

Thank you for the reply! Ah well that’s obvious now that you point it out. The sum is less than 9000 so it’s going to return the sum.

I messed with the code a bit but I’m still getting stuck.

def over_nine_thousand(lst): 
  sum1 = 0
  sum = 0 
  for num in lst: 
    sum1 = sum1 + num
    sum = sum + num
    if lst == []: 
      return 0
    elif (sum > 9000): 
      return sum
    elif (sum < 9000): 
      return sum1
      break

Where I’m having trouble with this getting the sum of all the objects in the list when the total sum is less than 9000. Again I know this is incorrect, but I’m not sure why.

You are not changing the list, so this is not something you would need to check multiple times. If it should happen once, then it shouldn’t be in a loop. Also, if that list is empty, how many iterations will the loop make, and what will the final sum then therefore be? So maybe it shouldn’t just be one time instead of many, but zero (indeed, zero is the first natural amount of times that you can do something)

If the list is empty, how many iterations will that loop make, and which return statement will the function therefore exit through, and what then becomes the result of the function?

Are there multiple reasons why you might want to stop the loop early? There seem to be two checks for doing that in the loop, but if you read the instructions, are there two reasons? And do those reasons match what you’re looking for?

What does breaking a loop do after you’ve exited the whole function?

Do the variables sum and sum1 ever have different values?

A lot of this can be avoided if you write down in English what actually needs to happen. Every piece of code should correspond directly to something you wrote in English. Figuring out what should happen isn’t something you do with the code, it’s something you do before writing code, and then you’d write the code for what you decided should happen.

Thank you for the thorough response. Okay I think I’m starting to see the many ways I confused this.

If the list is empty, the loop can’t make any iterations, and the sum would be zero default. I think what you’re getting at. So if the sum is zero, the for loop would jump to my elif (sum < 9000) line and return sum1. Is that correct?

For breaking the loop after exiting a whole function, I think you’re getting at that it’s redundant? Is breaking even necessary after you’ve returned a value?

To your last point, I’ve been coding it all backwards i.e. trying to figure out what should happen with the code instead of writing out what should happen before writing the code.

If you do zero iterations, then the things in the loop will not run

Yes, you need to already have decided on all the operations that should happen. You’ve got some information, in what ways do you wish to poke at that information? The more you ignore code while doing this, the better, though you will need to pay some attention to what actions you have the ability to express in code.


why does this show a result as None?

Hi @ajtkali2462741141 ,

What happens when this block executes?:

  while sum1 > 9000:
      break
      return sum1

it shows as None. but that is not the correct answer.

It does indeed show None, but that was not the question :wink:

@appylpye asks if you could explain the code he showed you. Please walk us through the steps this code takes

1 Like

Following are links to official documentation on break and return statements to aid in the discussion:

Consider how flow of execution is affected by each of those statements.

Something else to consider is that if a function terminates without execution of a return statement, None is returned by the function.

2 Likes

You will need one more return outside the for loop in case all the number has been added up and the sum is still less than 9000 and you need to return that value.

I used this thing
def over_nine_thousand(lst):
sum1=0
for i in lst:
if (sum1 < 9000):
sum1 += i
if(lst == [ ]):
sum1 = 0
return sum1

#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))

I solved it like this

#Write your function here
def over_nine_thousand(lst):
  a = 0
  while a < 9000:
     
    a = a + lst[0]
    lst.pop(0)
    print('a',a)
    print(lst)
  return a
#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))
#Write your function here
def over_nine_thousand(lst):
  total = 0
  if len(lst) == total:
      return total

  for i in range(len(lst)):
    total += lst[i]
    if total > 9000:
      return total
      break
    
  return total

#Uncomment the line below when your function is done
print(over_nine_thousand([8000, 900, 120, 5000]))`Preformatted text`

Here is my solution that fulfills all three requirements:

def over_nine_thousand(lst):
  lst_sum = 0
  for num in lst:
    if lst_sum < 9000:
      lst_sum += num
  return lst_sum

Here’s my version. Not saying it’s the best way to accomplish it, but it got the job done for all 3 requirements.

image

This is how I use While :

def over_nine_thousand(lst):
  summ = 0
  x = 0
  while x < len(lst) :
      summ += lst[x]
      x += 1
      if summ > 9000:
        break
  return summ

print(over_nine_thousand([8000, 900, 120, 5000]))
print(over_nine_thousand([]))
print(over_nine_thousand([7000, 100]))

#9020
#0
#7100

That’s a great work!
What I did is just use While loop to solve this and never think we can combine both For loop and While loop.
:clap:
I try your code and it still work good without (len(lst) > 0) because you had For l in lst which just have the same function.
Can you help me explain why you put Break inside the While loop ?

Hi y’all!

I have written a solution that gets accepted by the platform (it allows me to move on to the next exercise) but somehow throws "SyntaxError: unexpected EOF while parsing"?
Could someone shed a light?

Here is the code
def over_nine_thousand(lst):
  sum1 = 0
  for n in lst:
    sum1 += n
    if sum1 > 9000:
      break
  return sum1

That part is syntactically valid, maybe you also have other code that you’re not showing.

Oh, my bad. You are right.
It was as simple as forgetting the closing parenthesis in the print() function. I guess one gets so obsessed with the ‘main’ function that sometimes the silliest oversights happen elsewhere. Sorry!