-1

The following loop does not return a syntax error, instead it runs forever. Why?

x = 1
while x <= 10:
  print :x=x+1
  • 1
    You're assigning the result of `x+1` to the variable name `print`, type-hinted with `x` as a valid type. – deceze Sep 06 '22 at 07:24

1 Answers1

1

If we properly format that code, we get:

x = 1
while x <= 10:
  print: x = x + 1

So this is syntactically correct Python, but x is used as a type annotation (like x: int = 1).

Type annotations don't do anything on runtime but can be used by tools like mypy to help find bugs in your code.

So this code really boils down to:

while True:
    print = 2
Jasmijn
  • 9,370
  • 2
  • 29
  • 43