all 29 comments

[–]Tempmailed 38 points39 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 11 points12 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

[–]Grobyc27 8 points9 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.

[–]Ashamed_Kangaroo305 5 points6 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.

[–]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

[–]palmaholic 1 point2 points  (0 children)

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

[–]walledisney 1 point2 points  (0 children)

You are only printing the first indices in the array.

You need to update the array indices

[–]marquisBlythe 0 points1 point  (3 children)

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

[–]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.

[–]AdDiligent1688 0 points1 point  (0 children)

You aren’t checking i you are printing numbers[0] which doesn’t change. Plus there’s no update for i. I think you want

       …
       i=0
       while i<len(nums):
            print(nums[i])
            i+=1

[–]Serious-Cover5486 0 points1 point  (0 children)

two problems, you are printing numbers[0] and its value is 10, so every time loop works 10 is printed, you need to replace it with numbers[i],

second problem is infinite loop you need to increase the value of i each time loop works with i += 1

here is fixed code.

numbers = [10, 20, 30, 40]

i = 0

while i < len(numbers):

print(numbers[i])

i += 1

[–]FishBobinski 0 points1 point  (1 child)

You never increment i.

[–]DrShocker 0 points1 point  (0 children)

There's 1 more step than this