118
@airgap_amir ·

My while loop runs once and exits even though the condition is still true Help

I am walking through a list of numbers and stopping when I hit a negative one. The loop body definitely runs, I can see the print, but it exits after the first pass. The loop is while i < len(values): and inside I check the value, print it, and return if it is negative. It is all inside a function. I have checked the indentation about eight times.

8 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @goroutine_gil · 4mo ago · 3 replies

    Paste the real code if you can, but from that description your return is almost certainly at the wrong indent level, so it runs on every pass rather than only when the value is negative. That exits the entire function after one iteration, which matches what you are seeing exactly.

    Two other things while you are in there. Use break if you only want to leave the loop, and consider for value in values: which removes the counter entirely. The fact that you are not stuck in an infinite loop means your i += 1 is fine.

    96
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @goroutine_gil · 4mo ago

      Nobody sees their own indentation. It is the most common thing in this room by a distance.

      34
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @airgap_amir · 4mo ago

      It was the return. Lined up with the print, not inside the if. Eight readings and I did not see it.

      30
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @etcd_eli · 4mo ago · 3 replies

    Add print(i, values[i]) as the first line of the loop body and run it. Two seconds of output tells you more than twenty minutes of staring. One line means the exit is in the body; the same line forever means the counter is not moving.

    60
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
    • @etcd_eli · 4mo ago

      Print debugging is unfashionable and it answers this class of question in about four seconds.

      22
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
    • @airgap_amir · 4mo ago

      One line, then nothing. So the exit was in the body, exactly as described.

      18
      Share
      Reply

      Answering anonymously — a moderator will review it first.

      Report
  • @gitreflog_saved · 4mo ago

    return leaves the function. break leaves the loop. continue skips to the next iteration. Write those three on a sticky note, it saves a lot of confusion in the first few months.

    34
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @sheetsmith · 4mo ago

    If you are mixing tabs and spaces, your editor may be showing you something different from what Python sees. Set it to spaces only and turn on whitespace display.

    12
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report