0
>>> while True: print(count)
... count+=1
  File "<stdin>", line 2
    count+=1
    ^^^^^
SyntaxError: invalid syntax

I'm trying to do the loops exercise on the learnpython.org website and the problem i ran into is how to use the ":" correctly

Johan Buret
  • 2,614
  • 24
  • 32
  • Three problems: 1) If you write code after the `:` on the same line, you may not also put code underneath that goes inside the loop. For this code, `count += 1` will be outside the loop. 2) **at the interpreter prompt only**, you must use an extra blank line after a compound statement (a loop, function, `if` block, etc.), and may not have any blank lines inside the block. This is how the interpreter prompt knows that you are done typing the block so that it can evaluate the code. 3) There is nothing to make the loop stop, so you will need to force-quit the code with control-C. – Karl Knechtel Aug 27 '22 at 10:52
  • @JohanBuret This is not actually an indentation problem, per se; it would be reported that way. `while True: print(count)` is a complete loop; there cannot be a block after that, so the rest of the code should not be indented. The issue is that, at the interpreter prompt, there also can't be any "rest of the code" as we are interpreting one statement at a time. – Karl Knechtel Aug 27 '22 at 10:55
  • @KarlKnechtel simple problems deserve simple explanations. I edited my answer accordingly. – Johan Buret Aug 27 '22 at 11:00
  • how do i "force-quit" out of a loop, I usually just turn the command prompt off. – A random Bomb Sep 03 '22 at 11:05

1 Answers1

0

I see you are at this lesson : https://www.learnpython.org/en/Loops

One of the major point of Python is that indentation is syntaxically significant

https://www.learnpython.org/en/Hello%2C_World%21

Indentation

Python uses indentation for blocks, instead of curly braces. Both tabs and spaces are supported, but the standard indentation requires standard Python code to use four spaces.

This code block will work

    while True: 
        print(count)
        count+=1

Python likes its blocks. At the beginning do not try to put all statements in one line.

Johan Buret
  • 2,614
  • 24
  • 32