简体   繁体   中英

Why VS code shows it's invalid syntax for a python code using abc Module - python3.8.0

See code below, type the same as written in Book Data Structur and Algorthm in Python, I am new to programming, can someone help?

from abc import ABCMeta, abstractmethod

class Sequence(metaclass=ABCMeta):

    @abstractmethod
    def __len__(self):


    @abstractmethod
    def __getitem__(self, j): **# Here there is red wave line under 'def'**


    def __contains__(self, val):
        for j in range(len(self)):
            if self[j] == val:
                return True
        return False

    def index(self, val):
        for j in range(len(self)):
            if self[j] == val:
                return j
        raise ValueError('value not in sequence')

    def count(self, val):
        k = 0
        for j in range(len(self)):
            if self[j] == val:
                k += 1
        return k

In python Indentation is important also there is # used for comment

please go through the following code which is error-free

from abc import ABCMeta, abstractmethod

class Sequence(metaclass=ABCMeta):

    @abstractmethod
    def __len__(self):
        pass


    @abstractmethod
    def __getitem__(self, j):
        #**# Here there is red wave line under 'def'**
        pass
    def __contains__(self, val):

        for j in range(len(self)):
            if self[j] == val:
                return True
        return False

    def index(self, val):
        for j in range(len(self)):
            if self[j] == val:
                return j
        raise ValueError('value not in sequence')

    def count(self, val):
        k = 0
        for j in range(len(self)):
            if self[j] == val:
                k += 1
        return ks 

learning python for beginning use: learn python the hard way
or use python learning

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