32

I am studying the book "Head First Python" and I'm having trouble this code:

data = open('sketch.txt')

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

data.close()

Error:

  File "Aula 3.py", line 12
      print(role, end='')
               ^
SyntaxError: invalid syntax

sketch.txt:

Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!

I'm two days trying to figure out why the code is not working. The error is always shown in "end =''".

2
  • 4
    You're not using Python 3. Commented Nov 19, 2013 at 14:05
  • Install Python 3, and make sure you're running it Commented May 1, 2015 at 7:13

7 Answers 7

43

It seems like you're using Python 2.x, not Python 3.x.

Check your python version:

>>> import sys
>>> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
>>> print(1, end='')
  File "<stdin>", line 1
    print(1, end='')
                ^
SyntaxError: invalid syntax

In Python 3.x, it should not raise Syntax Error:

>>> import sys
>>> sys.version
'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]'
>>> print(1, end='')
1>>>
Sign up to request clarification or add additional context in comments.

2 Comments

@Venkatesh, By adding the following line at the first line of the code, you can use print as a function like Python 3.x: from __future__ import print_function
if you're sure about installing python3 you can be more explicit when calling it, i.e python3 program.py
9
>>> import sys
>>> print(sys.version)
2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]
>>> from __future__ import print_function
>>> print(1, end=',')
1,

Comments

4

If you're running it from the command line you may also just need to use the python3 command instead of just python command such as:

python3 MyFile.py

Comments

1

when you use eclipse editor,just typed like this,

from __future__ import print_function

for i in range(0,5):
    for j in range(0,i):
        print(j, end='')
    print()

Comments

1

If you are sure you have Python 3+ intalled, type this as first line in your .py file:

#!/usr/bin/python3.x  <<----- where x is your installed version. 

Example: I start the file with: #!/usr/bin/python3.5 (because I have 3.5 installed)

3 Comments

#!/usr/bin/python3.5
Why not just use #!/usr/bin/python3, which should be symlinked to a valid Python 3 version, making it more general? Anyway, this only works if you invoke it as ./myfile.py, not python myfile.py.
Or type "Python --version" into your command prompt and providing you have the environment variables setup you will see your version number.
1

I was having the same problem with Eclipse and IntelliJ IDEA, just solved it on Eclipse. Preferences with PyDev--interpreter had added Python 3.6 but the native Python on my Mac OS X was above the new install of the Python 3.6. So simply moving up the 3.6 in the interpreter solved my problem.

End result

Comments

1

Had the same issue while using Python 2.7 and 3.x parallel although 3.x is set as default. Because my PyInstaller from Python 3.x was removed (don't know why) the other one used. Reinstall PyInstaller solved the problem. Note that PyInstaller shows the used versions in the beginning of its output:

61 INFO: PyInstaller: 3.5
61 INFO: Python: 2.7.16

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.