FAQ: Code Challenge: Loops - Over 9000

Hello, @mauroborgesm.

Consider your while loop:

while new_list < 9000:

What if the list is empty or doesn’t have elements that will add up to 9000? How do you exit the loop?

Hi there! I used this as my solution to the challenge presented:

#Write your function here

def over_nine_thousand(lst):

lst_sum = 0

index = 0

while lst_sum < 9000:

   lst_sum += lst[index]

   index += 1

   if index == len(lst) - 1:

      index = 0



return lst_sum

#Uncomment the line below when your function is done

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

The answer comes out to the correct 9020, and no error is displayed, however the program returns this: over_nine_thousand([8000, 900]) should have returned 8900 , and it returned 8000. I was wondering if anyone could explain this phenomenon to me?

so first index is zero:

index = 0

then you increase the index by one:

index += 1

so now, index has a value of 1

then here:

if index == len(lst) - 1:

len(lst) is 2. so then we get:

if 1 == 2 - 1

doing the math:

if 1 == 1

1 equals 1 seems true.

Here is my solution.

def over_nine_thousand(lst):
  if not lst:
    return 0
  total_sum = 0
  for num in lst:
    total_sum += num
    if total_sum > 9000:
      return total_sum
    elif sum(lst) < 9000:
      return sum(lst)

Probably shouldn’t sum the whole list more than once.
Also, after finishing your loop you’ll have acquired the sum, so there’s no reason to use the built-in sum function

Thanks for the tip, I will try to optimize the code more.

Hi all,
Please could you let me know why this solution is not accepted? I’ve included a screenshot of my code below. It runs without errors and is returning the value expected - which is the sum of indices in the list starting at position 0 up to (and including) the value at index position n that results in the sum being greater than 9000.
I’ve probably missed something obvious and will be back to delete this post when I realise it! In the meantime, any assistance would be greatly appreciated.

To make matters more confusing - I noticed there might be an issue if the sum of all indices in the given list never exceeded 9000, so I added a ‘break’ command if the length of the remaining list was equal to 0. When I did this, the guidance at the foot of the screen told me the answer should be something different to what was outlined in the instructions (and what I was getting when I ran the program).

The instructions say:

if the list is empty, the function should return 0.

Your function returns an empty list when the list is empty (line 4 and 5)

Yes I see, obvious when you point it out…! Thanks

if you sum an empty list you get 0, that’s not a special case that you need code for

you don’t need to modify a list to read from it

removing at the front of a list is as much work as copying the whole list

Can anybody let me know why my code is not working as expected?

> def over_nine_thousand(lst):
>   sum1 = 0
>   i = 0
>   while sum1 < 9000:
>     sum1 += lst[0]
>     i += 1
>   return sum1

Hello, @dongnanchen490591588, and welcome to the forums. What are you using the variable i for?

Thanks… I think that is an error it should be below and it should work,

def over_nine_thousand(lst):

sum1 = 0

i = 0

while sum1 < 9000:

sum1 += lst[i]

i += 1

return sum1

Well, that’s an improvement for sure, but is the condition of your while loop going to work in all cases? What if the list were empty, or doesn’t contain values that add up to 9000 or more? Maybe a while loop isn’t the best tool for this task?

I was looking over the solutions by other people and noticed that mine didn’t use a while loop but was still correct. Is my code incorrect?

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

Hello, @uglyerick, and welcome to the Codecademy Forums!

The instructions ask for the total to exceed 9000. The following is part of the instructions:

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

What result does your function produce if sum becomes equal to 9000 before it has processed all the items in lst?

I see, thank you, so by changing it to >= my code would be correct even if it doesn’t use a while loop with a break as recommended by the hint?

Hi @uglyerick,

Are you sure that >= is the correct comparison operator?

To find out whether that works, test the function with a variety of cases, and display the results. Refine the code, if necessary. See the thread Help with over 9000 for some suggested cases.

Edited on May 24, 2020 to focus attention on the comparison operator