edwards
edwards

Reputation:

How to make this Python program compile?

I have this Python code:

import re
s = "aa67bc54c9"
for t, n in re.findall(r"([a-z]+)([0-9]+)", s)

And I get this error message when I try to run it:

  File "<stdin>", line 1
    for t, n in re.findall(r"([a-z]+)([0-9]+)", s)
                                                 ^
SyntaxError: invalid syntax

How can I solve this? I am new to Python.

Upvotes: 4

Views: 254

Answers (2)

Douglas Leeder
Douglas Leeder

Reputation: 53310

for starts a loop, so you need to end the line with a :, and put the loop body, indented, on the following lines.

EDIT:

For further information I suggest you go to the main documentation.

Upvotes: 7

Mark Rushakoff
Mark Rushakoff

Reputation: 258128

You need a colon (:) on the end of the line.

And after that line, you will need an indented statement(s) of what to actually do in the loop. If you don't want to do anything in the loop (perhaps until you get more code written) you can use the statement pass to indicate basically a no-op.

In Python, you need a colon at the end of

  • for statements
  • while statements
  • if/elif/else statements
  • try/except statements
  • class statements
  • def (function) statements

Upvotes: 4

Related Questions