How to nest in the While loop

I’m trying to use a while loop to sum the elements of a list until the sum is over 9000. My while loop isn’t working as it seems to completely ignore the inequality. I’ve tried changing the indentation of the return but that doesn’t make a difference.

#Wrong code using a while loop
def over_nine_thousand(lst):
  nine_thousand = 0
  while nine_thousand <= 9000:
    for i in lst:
      nine_thousand += i
  return (nine_thousand)

I managed to make a working version that doesn’t use a while loop:

#Working code
def over_nine_thousand(lst):
  nine_thousand = 0
  for i in lst:
    nine_thousand += i
    if nine_thousand > 9000:
      break
  return (nine_thousand)

Why is my while loop not working? Is it possible to write a version with a while loop?

Are you sure that equality is ignored? What output do you get and what were you expecting?
Edit: I’d also have a second look at your for loop only version (test it out a bit).

I’m pretty sure the inequality is ignored because the sum blasts straight past 9000.

With regards to my ‘working’ for loop, it worked when I tested it… What is wrong with it? :eyes: :sweat_smile:

I’m pretty sure I’m misusing the while loop in some way, because with this code to compare the numbers at the same index in two lists I have another problem with the script blasting past through the while loop

def same_values(lst1, lst2):
  lst_same = []
  point = 0
  while point <= len(lst1):
    if lst1[point] == lst2[point]:
      lst_same.append(lst1[point])
      point += 1
    else:
      point += 1
  return (lst_same)

“if lst1[point] == lst2[point]:
IndexError: list index out of range”

I realise there is a better way of doing this function - but I can’t get while loops to work.

Why are all my while loops wrong :sweat_smile: :sweat_smile: :sob: :sob:

Your operator <= is less than or equal to. How far past 9000 is it going?

Unless there’s a typo/indentation issue with your second loop I just copy/pasted and got-

over_nine_thousand([1])
Out: 1

Perhaps a little more testing is required, unless there is a typo somewhere.

1 Like

Sorry, I wasn’t clear. The idea is:

def over_nine_thousand(lst):
  nine_thousand = 0
  while nine_thousand <= 9000:
    for i in lst:
      nine_thousand += i
  return (nine_thousand)

print(over_nine_thousand([11,8000,990,40,79]))

The function would add numbers in a list until it was over 9000, but then stop. So this should have an output of 9001 , but instead has 9120.

1 Like

Ah I see. I took until the sum is over 9000 and the while loop to mean just keep adding elements from the list cyclically until the sum was over 9000. Sorry about that. You mean it’s perfectly valid for the sum to be below 9000 then.

In that case consider how often the while loop actually checks the turthiness of the expression. It occurs only every iteration.

But wouldn’t it still stop before the end if that was the case?

Have a quick test with a less complex example of your given loops.

lst = [1, 1]
total = 0
while total <= 2:
    for value in lst:
        total += value
print(total)

:face_with_head_bandage: :face_with_head_bandage: :face_with_head_bandage:

That comes out with a value of 4!

It does indeed. That’s why I called it a test rather than a soluton :+1:. Apologies for going around it a bit but I’m trying to get you to spot the issue.

The conditional for the while loop is checked once at the start of the while loop. If it evaluates to True then the code block grouped by the while statement will start to be executed. This conditional will not be checked again until all of the statements in this loop have been executed (barring any early exit statements or errors).

So -> check expression -> execute all the statements -> check expression -> … … repeat until expression == False

Right, thank you. Hence why your code runs no more than twice.
SOOOOO!!!::::
I just needed to rearrange it?

lst = [1, 1]
total = 0
for value in lst:
  while total <= 2:
      total += value
print(total)

Now I get 3!

And with my original …IT WORKS!!! EUREKA. I just had to rearange them :sweat_smile: :sweat_smile: Thank you so much for your time @tgrtim

1 Like

Before you skip this one entirely try doing it with just a while loop. Yes it can be a pain to get your head around but it’s better to do it now whilst it’s nice and fresh. There’s a few ways to do it but I think your best bet at this point might be making some use of the modulo operator %.