Build a Player Interface - Build a Player Interface

Tell us what’s happening:

i failed test case 17 which is ‘The Player class should be an abstract class.’ when i put an @abstractmethod decorator above the make_move method in the Player Class in line 11. When i remove it however, the code works and its accepted. Could someone explain the difference the decorator makes? My understanding is that adding the decorator to a method in the ABC results in the subclass needing to replace it? Any help would be greatly appreciated!

from abc import ABC,abstractmethod
import random

class Player(ABC):
    def __init__(self):
        self.moves=[]
        self.position=(0,0)
        self.path=[self.position]

    @abstractmethod
    def make_move(self)->tuple:
        move=random.choice(self.moves) #tuple
        self.position=(move[0]+self.position[0],move[1]+self.position[1])
        self.path.append(self.position)
        return self.position

    @abstractmethod
    def level_up(self):
        pass

class Pawn(Player):
    def __init__(self):
        super().__init__()
        self.moves=[(0,1),(0,-1),(-1,0),(1,0)] #up down left right

    def level_up(self):
        self.moves+=[(-1,1),(-1,-1),(1,1),(1,-1)]
        print(f'Leveled Up!New Moves {self.moves} avaiable!')

    def make_move(self):
        move=random.choice(self.moves) #tuple
        self.position=(move[0]+self.position[0],move[1]+self.position[1])
        self.path.append(self.position)
        return self.position   

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Player Interface - Build a Player Interface

If class is an abstract, all abstract methods have to be defined in the sub-classes. Otherwise it won’t be possible to create instance of such sub-class.

If you remove make_move method from Pawn class, while keeping it decorated with @abstractmethod, and then add

pawn = Pawn()

at the bottom of code, there will be error displayed.

That doesn’t mean all methods in an abstract class must be abstract methods. In Player class, only level_up method is expected to be an abstract method.

I see, thanks for clarifying!