data = open('sketch.txt')

for each_line in data:
    try:
        (role, line_spoken) = each_line.split(':', 1)
        print(role, end='')
        print(' said: ', end='')
        print(line_spoken, end='')
    except:
        pass

data.close()

I'M gettin the following error when I run this code but it looks exactly like what I'M seeing in my Head First book.

File "sketch.py", line 6
    print(role, end='')
                   ^
SyntaxError: invalid syntax


------------------
(program exited with code: 1)
Press return to continue

Add to beginning

from __future__ import print_function

You could use the Python2 style print, but learning Python3 style is worth it as it is more flexible and compatible with Python3.

Your "Head First" book must be using code for Python3. It might be worthwhile to install that version of Python.

Don't jump the gun and download Python 3.3 yet, it still has a few bugs.
Download the much more stable Python 3.2

Thanks. I do have python3 installed. So how do I point Geany to python3 rather than python 2 which it is currently pointing to. I looked through preferences but couldn't find it.

Thanks all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.