1
votes

I have no idea why I get a syntax error in my python code even though I checkt in all docs how the commands need to be. This is my code:

class Agent: # Agent class which pickle can dump to a file
   def __init__(self, name, userName):
      self.name = name
      self.context = ""
      self.conversation = ""
      self.userName = userName

   def save(self):  # Saves the agent object to a file
      file = open(f"{self.name}.mtrx", "wb")
      pickle.dump(self, file)
      file.close() 

And the error message is within this line:

    file = open(f"{self.name}.mtrx", "wb")
                                  ^
SyntaxError: invalid syntax

Is the error maybe somewhere else but the compiler spots it there or is actually something wrong about this line of code?

What version of python are you using? F-strings were only introduced in 3.6.Axe319
I'm using: Python 3.8.5Linda
You’re probably not. This error indicates you’re actually running a version too old to support f-strings.deceze
I happen to be running 3.8.5 as well. I just copied and pasted your code - no error. You should make sure you understand exactly what versions (there can be multiple) of Python are on your system, which one you are using, and how to control that.Karl Knechtel
You most likely think you are using Python 3.8.5, but you're using Python < 3.6, which doesn't support f-strings. Try adding import sys; print(sys.version_info) at the top of your script and you will see the version you are actually running.Marco Bonelli