 # How can we exit a loop?

This series of code worked after I took the elif statements out of the for loop and changed them to if statements.

Thank you for pointing me in the right directions

``````def over_nine_thousand(lst):
total =[]
for i in lst:
total.append(i)
if sum(total) >= 9000:
return sum(total)
if len(lst) == 0:
return 0
if sum(lst) < 9000:
return sum(lst)
``````
1 Like

…What’s the sum of an empty list?
And, if you’ve already determined that the total sum is at least 9000, do you need to test again whether it’s less than 9000?

Doesn’t it say OVER 9000? You seem to accept equal as well

I’m pointing out a whole lot of really obvious stuff that you’re very capable of reasoning about - most of the mistakes here come from doing reasoning that you’re already great at, but you’re doing it after you’ve already written it. If you reason out what should happen before writing it then those ideas are still in a form where you know exactly how to deal with them

You might for example start by writing out what should happen in plain english. yes it’s a bit silly but also forces you to go through it and mainly I only say write because that’s the easiest way to explain it, the important thing is thinking it through first

turns out we humans are pretty good at doing these manually, so that’s something you can use to process the problem before you move on to unfamiliar territory (code)

2 Likes

The directions on the exercise:
The function should sum the elements of the list until the sum is greater than `9000` . When this happens, the function should return the sum. If the sum of all of the elements is never greater than `9000` , the function should return total sum of all the elements. If the list is empty, the function should return `0` .

I wrote if statements that coincided with what the directions were asking. My experience is only a few months now with coding so I am quite literal when following directions.

I removed the equal sign on my condition.

1 Like

You may also find that if you do this manually, you won’t be trying to remember the individual numbers but instead only the current sum.

You would catch yourself doing this inefficient thing if you did it manually, yes?

Programming should really stay in that part of your reasoning. Same old every-day logic applies.

1 Like

I do write some of my code manually because it helps me think(especially my mistakes, when I am close to desired output).

If I don’t recognize something it is probably because I have not conditioned my brain through experience to do so. as I am still learning (albeit, by myself), getting this feedback is important.

1 Like

Not sure why the hint asks us to use break, when the following seems to work:

``````def over_nine_thousand(lst):
lst_sum = 0
for i in lst:
if (lst_sum < 9000):
lst_sum += i
return lst_sum
``````
1 Like

If I give that something similar to a list that is infinite, then it won’t terminate since it doesn’t exit early

1 Like

Isn’t that what the line

if (lst_sum < 9000):

checks for?

1 Like

The loop still continues.

Here’s such a value that you can try:

``````from itertools import repeat

over_nine_thousand(repeat(1))
``````

edit:
oops, should be `repeat(1)`, not `repeat()`, apparently. That’s what I get for posting things without testing them.

1 Like

Oh and you have a bug. Probably, anyway, didn’t actually try. This should result in 9001:

``````print(over_nine_thousand( * 10000))
``````
1 Like

Hello! First of all, the return is wrongly intended. It should be exactly below the if statement. But even if you do this , the code will display [8000, 900, 120] as a solution. This is because, you chose to return a list, instead of a variable.

total =

Considering what append.() does, can you now unterstand the error?

I please you to correct me if I’m wrong, i consider myself as an absolute beginner and it’s my first time advising someone else.

Hope i helped you. Happy coding!

1 Like

But if you want to follow the actual assignment and not just get the right result in different ways i suggest doing something like this:

``````def over_nine_thousand(lst):
su = 0
for n in lst:
su = su + n
if su > 9000:
break
return su
``````

I’m still working through this problem, however I can’t figure out why I’m getting a syntax error at the first elif statement. If anyone could help out I’d be very grateful!

This is the code I’m currently trying:

``````def over_nine_thousand(lst):
sumlst = 0
for num in lst:
if len(lst) == 0:
return 0
elif sumlst > 9000:
break
return sumlst
elif sumlst < 9000:
sumlst = sumlst + num
``````

The `SyntaxError` must occur at this line, which is actually the second `elif` header:

``````    elif sumlst < 9000:
``````

This is because that `elif` header follows a line that is indented to the same degree, which disconnects it from the preceding `elif` block. An `elif` block must always be associated with a preceding `if` or `elif` block at the same indentation level. Note that this does not mean that the preceding `if` or `elif` block cannot contain other conditional blocks.

There are additional issues that must be resolved in order for your function to produce the correct result. For example, if you increase the indentation of this line to make it part of the first `elif` block, it will never execute, because it is immediately preceded by a `break` in the same block:

``````    return sumlst
``````
1 Like

If I indent the 2nd elif block, I will get an indent error. I don’t get your explanation for the 3rd elif requiring an additional indentation… How does that make sense?

I worked through it again just now after taking a break, the challenge was doing my head in hahah.

This is the code that worked

Summary
``````def over_nine_thousand(lst):
sumlst = 0
for num in lst:
if len(lst) == 0:
return 0
elif sumlst < 9000:
sumlst = sumlst + num
elif sumlst > 9000:
break
return sumlst
``````

To overcome the `SyntaxError`, you would not indent the second `elif` block. You could instead further indent this statement, however, your function would still not produce a correct solution:

``````    return sumlst
``````

Since you have already revised your code, let’s consider what you have now. The instructions include this:

The function should sum the elements of the list until the sum is greater than `9000` .

Accordingly, if the sum has reached exactly `9000` and there still are additional items in the list that was passed to it, we need to continue at least to the next item.

What does the current version of your function compute if we call it as follows?:

``````print(over_nine_thousand([7000, 2000, 4000, 6000]))
``````

The correct result would be `13000`.

As you continue to work on your function, you could simplify it quite a bit. For example, you do not need to check for an empty list, as it is not a special case. In fact, these two blocks are both unnecessary:

``````    if len(lst) == 0:
return 0
elif sumlst < 9000:
sumlst = sumlst + num
``````

If you eliminate them, you’ll need to change this to an `if` header:

``````    elif sumlst > 9000:
``````

Edited on August 24, 2019 to add the following:

This statement, of course, still has an important role to play in a working solution:

``````    sumlst = sumlst + num
``````
2 Likes

Thanks for your reply! We don’t need to check for an empty list because sumlst would remain 0 correct?

Additionally, I didn’t understand how you would shorten the code by eliminating the two blocks you mentioned? Could you please clarify what you meant?

Thank you!

Correct.

Three steps for solving the problem:

• initialize `sumlst` to `0`
• execute the loop to total up values of items
• `return sumlst`

Inside the loop, we add values of items to `sumlst`, one value per iteration. Only one test needs to be performed. That is to find out whether the value of `sumlst` has exceeded `9000`. If that has occurred, we `break` out of the loop. Only one conditional block, an `if` block, is needed to accomplish this.

This is the code I’m working on. I know now that it’s clunky and has unnecessary lines to it, but I still don’t understand why it’s not working. When I run it, it says it returned 8000 instead of 9020. I don’t understand why the for loop is only running through the first object and not the rest of them. What am I missing here?

Hello, @dev4907005640.

Welcome to the forums.

Inside your `for` loop, you have this:

``````if(sum < 9000):
return sum
``````

The first value in the list `lst` is presumably `8000`. `8000` is less than `9000`, so what happens?
What is the value of `sum` the first time this code is executed?
What does `return` do?