0

My code:

class Book:
    all = []
    def __init__(self, ISBN, title, subject, publisher, language, number_of_pages):
        self.__ISBN = ISBN
        self.__title = title
        self.__subject = subject
        self.__publisher = publisher
        self.__language = language
        self.__number_of_pages = number_of_pages
        self.__authors = []

        Book.all.append(self)

    def __repr__(self):
        return f"Title {self.__title} Subject {self.__subject}"

b = Book(23498723,"this","fiction","penguin","en",196)

Error:

    return f"Title {self.__title} Subject {self.__subject}"
                                                          ^
SyntaxError: invalid syntax

Could anyone explain why this is an error?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Raj
  • 3,637
  • 8
  • 29
  • 52
  • 4
    Perhaps you are using an older Python version than 3.6. – mkrieger1 Jul 20 '23 at 20:23
  • 2
    The class and everything else seems to be a red herring. You'd probably get the same error if your script were just the f-string. In the future, please isolate the problem in a [mre]. – wjandrea Jul 20 '23 at 20:30

1 Answers1

2

Your Python vesion does not support "f-strings".

"The release of Python version 3.6 introduced formatted string literals, simply called f-strings."

You can upgrade to python version 3.6 or the latest version. Download and use python version 3.6 or later from this link: https://www.python.org/downloads/release/python-3615/

Revisto
  • 1,211
  • 7
  • 11
  • Thanks, interestingly, VSCode chose to run 2.7 instead of the 3.9 I generally use. – Raj Jul 20 '23 at 21:44