end='' SyntaxError: invalid syntax - python-3.x

Help! end='' is causing a syntax error and I'm not sure why.
def print_game_board(game_board_marker):
for i in range(len(game_board_marker)):
for j in range(len(game_board_marker[i])):
print(str(game_board_marker[i][j]).rjust(4), end='')
print()
print()

It seems like you're using Python 2.x, not 3.x.
Check your python version:
$ python -V
Python 2.7:
>>> print(1, end='')
File "<stdin>", line 1
print(1, end='')
^
SyntaxError: invalid syntax
Python 3.3:
>>> print(1, end='')
1>>>

Related

Strange behavior of Python (IndentationError: unexpected indent) - Why?

I have a strange error. Why does it happen?
I write code but Python shows me the error, but I don't see that error. I've attached the screenshot.
Code:
def func(q):
def funct(y):
try:
print(y)
exit()
except:
pass
funct(q)
a=['1','2','3','4','5','6','7']
for x in a:
func(x)
Python:
>>> def func(q):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> def funct(y):
File "<stdin>", line 1
def funct(y):
^
IndentationError: unexpected indent
>>> try:
File "<stdin>", line 1
try:
^
IndentationError: unexpected indent
>>> print(y)
File "<stdin>", line 1
print(y)
^
IndentationError: unexpected indent
>>> exit()
File "<stdin>", line 1
exit()
^
IndentationError: unexpected indent
>>>
>>> except:
File "<stdin>", line 1
except:
^
IndentationError: unexpected indent
>>> pass
File "<stdin>", line 1
pass
^
IndentationError: unexpected indent
>>>
>>> funct(q)
File "<stdin>", line 1
funct(q)
^
IndentationError: unexpected indent
>>>
>>> a=['1','2','3','4','5','6','7']
>>> for x in a:
... func(x)
...
>>>
Notepad++:
It looks like you're copying and pasting the code into your command prompt, which is why its throwing you an error. When typing code in the "interactive python" command line, if you leave a blank line Python interprets that as you having finished writing code, and executes it.
You're much better off saving your code in a .py file, and then in the command prompt using the command
python folder/to/your/file.py
which will run your code, and allow blank lines. See this question for more information.
You could also change directory by using the cd command in command prompt like so
cd folder/to/your
which will "move" your command prompt into the folder containing your python file. That way if you want to run your code, you only need to use:
python file.py

Learn Python the Hard Way -- ex17.py under python-3 environment

I try to learn this book under python-3 environment but it pops out an error when I try to run it. Is there anywhere to fix this?
Once I deleted
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
The code ran perfectly. So I think it should be the syntax problem between python 2 and python 3
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print("Copying from %s to %s" % (from_file,to_file))
# we could do these two on one line too, how?
in_file = open(from_file)
indata = in_file.read()
print("The input file is %d bytes long" % len(indata))
print("Does the output file exist? %r" % exists(to_file))
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done")
out_file.close()
in_file.close()
When I try to run it, it should stops at input() and wait me hit the return key to continue. But in reality, the code stopped and an error called
"Traceback (most recent call last):
File "ex17.2.py", line 18, in <module> g
input("")
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
popped out.
Yes you are correct!! The error you are getting is due to python version change. This code works perfectly fine in python 3.x but fails in python version under 3.x.
Hope this helps.

Python- Runtime error(NZEC)

Question-
https://www.codechef.com/problems/SINS
Compiler used-
https://www.codechef.com/ide
Input method-
Custom input of codechef compiler.
My attempt-
T=int(input())
def fnc(a,b):
if b!=0:
return fnc(b,a%b)
else:
return int(a*2)
while T>0:
X,Y=map(int,input().split())
if X==0:
print(Y)
elif Y==0:
print(X)
elif X==Y:
print(X*2)
else:
f=fnc(X,Y)
print(f)
T=T-1
Issue:
I am getting the following runtime error:
Traceback (most recent call last):
File "./prog.py", line 8, in <module>
EOFError: EOF when reading a line
The output is correct but still there is this runtime error.
That is because the input() is getting End Of File (I assume you are using Python 3). You need to trap the EOFError exception and break out of the loop when you get it. You asked what I meant by that comment, this is what I mean:
try:
X, Y = map(int, input().split())
except EOFError:
break

Taking input while accounting for both Python 2.x and 3.x

Since Python 2's raw_input was changed to just input for Python 3, I was wondering if there was a way to take input while accounting for both Python 2 and 3. I'm trying to write a script for both versions, and this input part is the only thing that's not working well.
I tried running just input with Py2, and this happens:
>>> a = input('Input: ')
inp: test
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a = input('Input: ')
File "<string>", line 1, in <module>
NameError: name 'test' is not defined
I saw a workaround to quote the input:
>>> a = input('Input: ')
inp: "testing test"
a
'testing test'
Is there a way to concatenate the quote to the beginning and end of the input? '"' + input('input: ') + '"' isn't working
Probably not a good practice, but you can use a try block to test whether the raw_input() is being recognized (thus telling you whether you're on Python 2.x or Python 3.x):
try:
a = raw_input('input: ')
except NameError:
a = input('input: ')
I would use raw_input() to test because it's not accepted by Python 3.x and is what you expect to use in Python 2.x, so the input() will not trigger in Python 2.x.
I'm not an expert so I'm sure there are better suggestions.
#Chris_Rands' suggested dupe thread has a more elegant solution by binding input = raw_input so if you have multiple input(), you only need to try once.
This works for both Python 2 and 3.
from builtins import input
input("Type something safe please: ")

SyntaxError: unexpected EOF while parsing in Python 3

Code :
n,X=input(),0
for t in range(int(n)):
eval(input())
print(X)
Traceback (most recent call last):
File "prog.py", line 3, in <module>
eval(input())
File "<string>", line 1
X++
^
SyntaxError: unexpected EOF while parsing
Using raw_input instead of input() in the only solution I am able to find but in python 3.x input is raw_input(): How do I use raw_input in Python 3
any other method?
FYI; I am trying to solve: http://codeforces.com/problemset/problem/282/A
Remove the eval() call.
input() in Python 2 is the equivalent of eval(input()) in Python 3, and if you need to use raw_input() in Python 2 then in Python 3 you need to remove the eval() call.
You'll have to parse the input yourself; ++ is not a valid Python operator, you cannot use eval() to solve that Codeforces problem.
The simplest way to solve the posted problem is to read the input line by line:
import sys, itertools
count = int(next(sys.stdin))
x = 0
for line in itertools.islice(sys.stdin, count):
x += 1 if '++' in line else -1
print(x)

Resources