Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SyntaxError
#1
Hi

Could someone identify the reason for my syntax error in the python2 script please?

Error:
File "init_collapse.py", line 223 print(("%d %d %d") % (shape(kxx)[1], shape(kxx)[0], collapse_steps), file=f) ^ SyntaxError: invalid syntax dave@dave-P17FV7:~/Documents/COMCOT$
Yoriz write Mar-16-2022, 06:22 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Probably the line above it, or it could be an indenting error. Nothing wrong with the statement itself.

That is a particularly awkward looking statement. I really dislike that old style of formatting. If you are using Python 3.6 or newer you could write it like this:
print(f"{shape(kxx)[1]} {shape(kxx)[0]} {collapse_steps}", file=f)
You gotta move up from 2.7.
Reply
#3
Thank you. I'm just trying to get a script written by someone now deceased to work, I have no real python scripting skills. This is the line above, is the syntax error in this?
f=open(file_ls,"w")
	print(("%d %d %d")%(shape(kxx)[1],shape(kxx)[0],collapse_steps),file=f)
Many thanks.
Yoriz write Mar-16-2022, 07:58 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
Should be this:
f=open(file_ls,"w")
print(("%d %d %d")%(shape(kxx)[1],shape(kxx)[0],collapse_steps),file=f)
Python 2.whatever might be more forgiving about indentation, and the error might even be before this.
Are you changing Python versions? You say this is a Python 2 script. Are you using Python 2 or Python 3?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020