-4

I think the piece of code below is written in python 3 and my python 2 cannot run it. There is some problem with 'end'. How could I fix it? I don't know what is the logic behind end an i am very new to python

Any help much appreciated!

def myPrint(itp):
    for i in range(10):
        print("**",end=="")
        for j in range(10):
            print(itp[i][j],"**",end=="")
        print()
  • "some problem"... Have you tried reading about Python 2's `print` statement and Python 3's `print` function? It looks like you got an error message and immediately asked a question without doing any research (which you are expected to do before asking a new question). – TigerhawkT3 Nov 28 '16 at 01:24

2 Answers2

0

You have two errors in your code, you need to replace both end=="" with end="". You are not supposed to compare the parameter end with an empty string, but you want to end the printing with an empty string, hence, do an assignment to the parameter end.

In Python 3.x, the end='' part will place a whatever parameter end is assigned with (here, an empty string) after the displayed string instead of a newline.

If you want to have the print functionality of python3 in python2, simply do an import :

from __future__ import print_function
Jarvis
  • 8,494
  • 3
  • 27
  • 58
0

(I am assuming the double == after end is a typo. end as a kwarg determines the end of line; the code likely originally was print("**",end="") with one = character)

To get print function semantics, you can set the __future__ flag print_function, by starting your file with

from __future__ import print_function

Be aware that the changes required to run a full Python 3 program under Python 2 are far more extensive than simply enabling some future flags (while you're add it, consider enabling unicode_literals). By far the easiest way to run a Python 3 program is to install a Python 3 interpreter.

Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469