简体   繁体   中英

Invalid syntax error using Python and classes in Jupyter Notebooks

Hi there I have the following code inside Jupyter Notebooks using Python 3:

Python code inside Jupyter Notebooks

When I type the same code into Python shell the same thing happens:

class SARDog(Dog):
    """Represent a search dog."""

    def__init__(self, name):
        """Initialize the sardog."""
        super().__init__(name)

    def search(self):
        """Simulate searching."""
        print(self.name + " is searching.")

    my_dog = SARDog('Willie')

    print(my_dog.name + " is a search dog.")
    my_dog.sit()
    my_dog.search()

    SyntaxError: invalid syntax

-

The red line displaying the syntax error comes next to the colon after (self, name):

I have tried restarting the kernel in Jupyter but obviously the code is having the same effect in another environment.

The code is from a beginners Python cheat sheet.

Will really appreciate any input thanks!

After the __init__ you need to add

self.name = "your choice, can be a 'str' though"

So that self.name is defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM