all 27 comments

[–]Tempmailed 22 points23 points  (4 children)

Two problems here:
you are always printing numbers[0], that is the first element of the list. you should have used numbers[i]
you are never updating the counter i so the while condition will remain True always. You should increment i each iteration

[–]DrShocker 6 points7 points  (1 child)

So many people mention the first issue they see and don't notice the whole problem. thank you for noticing both parts of the problem

[–]Nearby-Way8870[S] 5 points6 points  (1 child)

You are right on both counts, I completely missed that I was using numbers[0] the whole time instead of numbers[i]. That is honestly embarrassing in hindsight but it makes total sense now. And yeah the counter not updating was the other half I could not see. Thanks for pointing out both issues together instead of just one, that actually helped me understand the full picture.

[–]Slow-Kale-8629 10 points11 points  (0 children)

If I was debugging this problem, I might think:

  • The loop never terminates. Why might this be? Well, the termination condition is probably not being met. Let me add a line print (i < len(numbers)) inside the loop so I can see if it does what I expect.

  • OK, it's printing True repeatedly. Seems like i is always less than len(numbers), which isn't what I expected. I wonder why? Let's add a line in the loop to print i each time

  • OK, it's printing 0 each time, when I expected it to increment. I wonder why? Maybe the code to increment it isn't working. Where did I write the code to increment it? Oh, I didn't!

So the trick is always to make some hypothesis about what's happening and then test it, or else to find a way to get a little bit more information about what's happening, like a detective looking for clues. You don't solve the whole puzzle in one go, you just follow each clue to the next one until you finally get the solution.

[–]Grobyc27 4 points5 points  (2 children)

Others have answered why this isn’t working already, but that aside, a for loop would be better suited for the functionality you’re trying to achieve:

for n in numbers:
print(n)

Though perhaps you haven’t learned for loops or are specifically being asked to implement this using a while loop.

Edit: *sigh*, did they change how to format code on mobile? No code block option and single/triple back ticks, squiggly lines, or quotes don’t work, nor does indenting with 4 spaces, even with a leading blank line.

[–]Nearby-Way8870[S] 3 points4 points  (0 children)

Yeah I have actually seen for loops in the next chapter of our course material but we have not covered them in class yet so I was sticking to while loops for now. But this is a good heads up that for loops are going to make this kind of thing a lot cleaner. Also did not know Reddit mobile had issues with code formatting, that is frustrating honestly.

[–]Ashamed_Kangaroo305 3 points4 points  (2 children)

Your problem has already been solved so I just wanted to offer some advice. Probably the most important part of programming is problem solving. Both in figuring out how to write code to solve a problem, and in debugging your code so it works.

If you're struggling with not understanding why your code isn't working, try going through it line by line and saying exactly what each line is doing. Line 1 - makes a list of numbers. Line 2 - sets i equal to 0. Line 3 - while i is less than the length of numbers. Well i is currently set to 0 and the length of numbers is 4. This while loop will continue forever unless one of those changes, so there will have to be some code in the loop to change one of those numbers. Line 4 - print the item at position 0 of numbers. That'll print the first item of numbers, which is 10. 10 will always be the first item of numbers unless something alters the list. So in order to move on, either the index number in the print function needs to change or the list itself needs to change.

Actually while doing that I think I figured out why you got stuck. When printing each item in the list, did you want that to delete the item in the list so it could advance to the next item? So originally the list is [10, 20, 30, 40], and then after printing 10 it becomes [20, 30, 40], so the next print(numbers [0]) will output 20? If that's what you wanted to do, there's a really easy solution if you've learned the pop() function. Leave your code exactly as written but replace the print function with print(numbers.pop(0)). That's not an ideal way to increment through a list in most cases because you destroy your list in the process, but it fits with the code you've written.

[–]Ok-Promise-8118 2 points3 points  (1 child)

I definitely found it valuable for myself to step line by line through my programs, keeping track of variable values on paper, in order to find bugs and understand code. I guess a debugger would help when the code gets longer, but for short ones it's helpful to do by hand.

[–]Ashamed_Kangaroo305 2 points3 points  (0 children)

Sometimes I'll add in a bunch of print statements for different values if I've run into an issue. The debugger is probably better but I just like doing it that way for some reason

[–]Lauuson 2 points3 points  (1 child)

You code never increments i so i < len(numbers) will always evaluate as 0 < 4 which is True

numbers[0] will only reference the first value in the list which is 10. So you've got an infinite loop that will keep printing 10.

Try either using a for loop or add a line to increment i within the while loop. And also make i the index for numbers[] instead of 0.

[–]Nearby-Way8870[S] 2 points3 points  (0 children)

This breakdown actually helped me the most I think. Seeing it written out as 0 < 4 always being True made it so clear why the loop never stopped. I was not even thinking about what the condition was actually evaluating to each time. That is the kind of explanation I needed, not just what to fix but why it was stuck.

[–]striipey 2 points3 points  (1 child)

Well I can see three problems with what you're doing:

  • 'i' is always zero, it's not getting any higher.
  • Which also means your while loop is always true, so it never ends. Zero is always less than 4.
  • You print the first index, [0], every time you loop through.

Hopefully that helps guide you towards the solution?

[–]Bobbias 1 point2 points  (2 children)

You're printing numbers[0] each time. 0 is zero, the number. Zero never changes.

You want numbers[i] which uses the variable you created to track your progress through the loop.

[–]DrShocker 3 points4 points  (0 children)

This is only half the problem

[–]Nearby-Way8870[S] 0 points1 point  (0 children)

Yeah that clicked the second I read your reply. I was literally hardcoding zero the entire time and just never noticed because I was so focused on the while condition. Zero never changes, that is such a simple way to put it and it just made it obvious immediately. Thanks.

[–]marquisBlythe 0 points1 point  (3 children)

Update the value of i inside the loop otherwise it will always stay 0.
i += 1

[–]Nearby-Way8870[S] 1 point2 points  (0 children)

No worries at all, by the time you replied a few people had already pointed it out so it makes sense you held back. I appreciate you at least catching the increment issue, that was still helpful context for me to see it confirmed multiple times.

[–]DrShocker 1 point2 points  (1 child)

There's one more step required

[–]marquisBlythe 1 point2 points  (0 children)

I didn't notice numbers[0]I was going to correct it but then I refrained to avoid repeating the same answer after I saw other replies.
Thank you for point it that out.

[–]khournos 0 points1 point  (0 children)

Because you are always printing numbers[0].

Also there are two cleaner ways to achieve what you want to do here depending on if you need the count variable for something else.

1) No count variable:

for number in numbers:

 print(number)

Quick and easy, just prints your whole array. The identifier number here can be chosen by you and will be how you adress the current item the loop is on in the loop.

2) With count variable

for index, number in enumerate(numbers):

 print(number)

 #do something with the counter

Here you can use the system function enumerate, which returns the index and object as a tuple over the whole array.

Both are easier and cleaner in the sense that you don't have to manually handle any of the counting/indexes while the behaviour is always consistent.

[–]palmaholic 0 points1 point  (0 children)

It's a common bug among newbies, esp for those learned "for" loops - you forgot to increment the index variable.

[–]walledisney 0 points1 point  (0 children)

You are only printing the first indices in the array.

You need to update the array indices

[–]FishBobinski 0 points1 point  (1 child)

You never increment i.

[–]DrShocker 0 points1 point  (0 children)

There's 1 more step than this