Issue with printing without creating newline in Python when running from terminal - python

I am just starting up with Python and was trying to figure out how to print the contents of a list of items without getting a newline for each new item printed. I've looked around on the web for solutions, one of which being replacing print(x) with print(x, end="") when printing an item x. My simple code is as follows:
list = [1, 2, 3, 4, 5]
for x in list:
print(x, end="")
I'm of course expecting the simple output of
12345
which I get when i run the script in PyCharm, but when I try to run the code from the terminal I get the error message:
print(x, end="")
^
SyntaxError: invalid syntax
Why is this happening? I am running Python 3.6 by the way.

This syntax is available only in Python3. You need to check your Python version again because it has to work as you see below:
>>> python test.py
File "test.py", line 4
print(x, end="")
^
SyntaxError: invalid syntax
>>> python3 test.py
12345

a=[1,2,3,4,5]
print(*a,sep='')
output
12345

Related

how to desactivate double indent when I paste the code in a python terminal

I try to understand why my pasting in my terminal (linux) in my python interpreter put double indent every line for example :
if I paste these lines :
def function(string):
final = ''
for i in string:
try:
final += str(int(i))
except ValueError:
return int(final)
I'll get this in my terminal :
ent>>> def function(string):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> final = ''
File "<stdin>", line 1
final = ''
^
IndentationError: unexpected indent
>>>
>>> for i in string:
File "<stdin>", line 1
for i in string:
^
IndentationError: unexpected indent
>>>
And more and more... because when there is a for or any instruction that needs a tab, it return it directly instead to wait for the next instruction,so it rise an IndentationError:
And I realize that if I paste this for exemple :
import mathimport threading
it will paste like this in my terminal :
>>> import math
>>> import threading
>>>
I didn't have from the beginin, so any idea ? because it was impossible to find my answer in internet..
They are mixed, if we see my first code, it's only one indentation, and then the second line is one tab and one indentation, and from the fourth line it's two tabs, as simply as we see on my question.
And if I paste it in any other text editor it work perfectly, I tried to see every setting in my terminal or in my linux but I've found nothing so far... It's really strange !
Edit :
Apparently for a reason that I don't know, it come from Atom the editor that I use for writing my python code.
I explain it's very strange !
if I copy the code from Atom, and I paste it in any other text editor (there, in kate or other) and I copy from this other editor and paste in my terminal, it's happend again, double indendation.
But to correct that I just create a new fil in Atom, and then is fine, It work properly the pasting in the terminal.
Question solved, but why ?? That's a mystery ^^

EOF when reading a line in competitive website, namely Code Chef

I'm trying to solve a problem using the following code:
X,Y = map(float, input().split())
if X < Y and not X % 5:
print(Y - X - 0.50)
else:
print(Y)
This code gives me the desired output when I run using IDLE. However, when I try running this code using an interpreter provided by a competitive programming website, I get the following error:
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line
I tried reading the answers of other similar questions, but none of them seemed to work in my case.
I am not sure of the reason but the program is trying to read after the end of the data. You can solve the problem by exception handling
try:
data = input()
except EOFError:
break
Take another look at the codechef page. Notice the checkbox marked 'Custom Input'. With that checked/ticked a textbox will open where you can put your input lines.
The competitive programming website is likely running python 2. Python 2 treats input() differently than python 3.
You should rather use raw_input() than input().
From the docs:
raw_input() reads a line from input, converts it to a string
(stripping a trailing newline), and returns that.
Your problem can be explained from what was explained here:
In Python 2, raw_input() returns a string, and input() tries to run
the input as a Python expression.

IPython Notebook image loading syntax

I'm taking this Coursera course on linear algebra, which is Python based. I got so far as to produce some initial plots and work with complex numbers on IP Notebook. I am using Windows, and the display of the Notebook is on Chrome.
I got to a command to call a jpeg image on the plot and the line written by the instructor is:
from image import *
I am getting the following error message:
In [7]: from image import *
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
I believe that the call may be pointing to the image.py downloaded from this site.
I'll go and assume that in the course they're working with python 3, so the error you're getting is because of the print function.
from image import *
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
Either use Python 3, or from __future__ import print_function
It looks to me like the code is designed for Python 3 and uses Python's print function. Anaconda provides Python 3, so try using that instead -- I can import image fine that way.
The precise location of the SyntaxError when I use Python 2 is here:
File "image.py", line 98
print("Hit Enter once the image is displayed.... ", end="")
^
SyntaxError: invalid syntax
which happens because Python2 is trying to print a tuple of values but the second element it gets for the tuple is an assignment.
The docs for your Coursera course tip you off about this (although it doesn't seem obvious) and warn you to use Python 3 and not Python 2.

Need syntax help for python caeasar cipher one liner with input from bash echo command

I am attempting to write a python one liner for a caeasar cipher the takes input from echo and shifts it 3 characters. when I run it I get a syntax error message. I would appreciate it if someone could point out where I am getting the syntax wrong. I am using python 2.6 on cent os 6.
~ $ echo "HELLO" | python -c "import sys; print ' '.join(chr(ord(line)+3ys.stdin])"
File "", line 1
import sys; print ' '.join(chr(ord(line)+3)[for line in sys.stdin])
^
SyntaxError: invalid syntax
Of course the out put should print: KHOOR.
Thank you.
The immediate syntax error is because of the square brakets you're putting around for line in sys.stdin. Those are unnecessary and should simply be dropped.
However, you're still going to have an issue with your code, because you're calling ord on a full line, not just a single character. You probably need an additional loop to iterate over the characters of each line. In the following code, that's what I do, with the further addition of stripping the line so that we don't try to shift the newline character to something strange:
import sys; print "\n".join("".join(chr(ord(char)+3) for char in line.strip()) for line in sys.stdin)
I think what you want is
import sys; print ' '.join([chr(ord(line)+3) for line in sys.stdin])
^
Doc for list comprehensions.

Python invalid syntax in print

Sorry I am not familar with Python...
It gives me the following error message
File "gen_compile_files_list.py", line 36
print 'java files:', n_src
^
SyntaxError: invalid syntax
I.e. caret points to last quote. What's wrong with it?
OS Windows 7, Python version 3.2.2
On Python 3, print is a function. You need this:
print('java files:', n_src)
print changed syntax between Python2 and Python3; it is now a function.
You would need to change:
print 'java files:', n_src
to
print('java files:', n_src)
Alternatively, you can try converting the code from Python2 to Python3 syntax with the 2to3 tool. Here is more information on the transition if you are interested. This way you can maintain a single code base that works on both versions.
As you are not familiar with python, try installing Python 2 instead and running the code with that.
print is a function in Python 3+. So:
print ('java files:', n_src)

Categories

Resources