Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SyntaxError: invalid syntax end=''

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

like image 734
Jonny Avatar asked Nov 19 '13 14:11

Jonny


People also ask

How do I fix invalid SyntaxError in Python?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What can I use instead of end in Python?

It was optional in Python 2. The print() function inserts a new line at the end, by default. In Python 2, it can be suppressed by putting ',' at the end. In Python 3, "end =' '" appends space instead of newline.

How do you stop a SyntaxError?

How to Fix It: If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.

Why does Python keep saying invalid syntax?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.


2 Answers

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>>> 
like image 120
falsetru Avatar answered Oct 06 '22 02:10

falsetru


>>> 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, 
like image 21
Teddy Avatar answered Oct 06 '22 01:10

Teddy