添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 =''".

>>> 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='')
                @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
– falsetru
                Apr 8, 2014 at 23:46
                if you're sure about installing python3 you can be more explicit when calling it, i.e python3 program.py
– Karuhanga
                Nov 7, 2018 at 11:54
>>> 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=',')

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

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)

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. – Arya McCarthy Jun 11, 2017 at 6:50 Or type "Python --version" into your command prompt and providing you have the environment variables setup you will see your version number. – Hutch Apr 11, 2019 at 14:39

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
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.