0

I'm getting syntax error in this particular line.

print "Sense %i:" %(i),

Full code:

for i in range(len(meas)):
    p = sense(p, meas[i])
    r = [format(j,'.3f') for j in p]
    print "Sense %i:" % (i),
    print r,
    entropy(p)
    p = move(p, mov[i])
    r = [format(j,'.3f') for j in p]
    print "Move %i:" % (i),
    print r,
    entropy(p)
    print
5
  • Which is the version of Python you're using? If it's Python 3 your code will have to look like this: print("Sense %i:" % (i), end=""). Commented Oct 11, 2016 at 21:27
  • When i try making changes in the code i get error in next line Commented Oct 11, 2016 at 21:34
  • In that case you might be missing a closing parentheses in the line before this. Commented Oct 11, 2016 at 21:40
  • for i in range(len(meas)): p = sense(p, meas[i]) r = [format(j,'.3f') for j in p] print "Sense %i:"%(i), print r, entropy(p) p = move(p, mov[i]) r = [format(j,'.3f') for j in p] print "Move %i:"%(i), print r, entropy(p) print Commented Oct 11, 2016 at 21:59
  • What is the error message? Post the full stack trace. Commented Oct 12, 2016 at 8:11

3 Answers 3

1

Several things:

  • In Python 3, print is a function, no more a statement like it was with Python 2. So, you need to add parenthesis to call the function,
  • the % operator used for string formatting is deprecated. With Python 3, you should use the format method (or the format function),
  • the % operator usually take a tuple as a second parameter: the expression "(i)" is not a tuple but a constant. With Python, the singleton tuple has a trailing comma like this: "(i,)".
  • use the keyword argument end="" to replace the newline by an empty string (but I'm not sure this is what you want)

So, you can replace your code by:

print("Sense {}:".format(i), end="")

EDIT: add code from comment

Your code should be converted in Python 3 like bellow:

for i in range(len(meas)):
    p = sense(p, meas[i])
    r = [format(j,'.3f') for j in p]
    print("Sense {0}:".format(i), end="")
    print(r, end="")
    entropy(p)
    p = move(p, mov[i])
    r = [format(j,'.3f') for j in p]
    print("Move {0}:".format(i), end="")
    print(r, end="")
    entropy(p)
    print()
Sign up to request clarification or add additional context in comments.

2 Comments

for i in range(len(meas)): p = sense(p, meas[i]) r = [format(j,'.3f') for j in p] print "Sense %i:"%(i), print r, entropy(p) p = move(p, mov[i]) r = [format(j,'.3f') for j in p] print "Move %i:"%(i), print r, entropy(p) print
Thanks a lot !!! it works perfectly fine now... Actually i am just beginner in python programming. I would like to know if you can help me with other codes. Thanks again.
0

Try this:

print ("Sense %s:" %i)

Will work just fine

Comments

0

In python 3, print is a function, you have to use brackets: print("Sense %i:" %(i))

1 Comment

print ("Sense %i:"%(i)) print r, entropy(p) p = move(p, mov[i]) r = [format(j,'.3f') for j in p] print "Move %i:"%(i), print r, entropy(p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.