Почему я получаю эту ошибку и как ее решить?

#python

Вопрос:

Я выполняю упражнение по ООП и не могу исправить эту ошибку. Если бы кто-нибудь мог мне помочь и проверить, правильно ли я написала свой код, это было бы очень полезно.

 Traceback (most recent call last):
  File "./main.py", line 12, in <module>
    baldosa.rotar(270, "horario")
AttributeError: 'Baldosa' object has no attribute 'rotar'
 
 class Camino:
    def __init__(self,extremos,color):
        
        self.extremos = extremos
        self.color = color
        self.conquistado = False
        
    def __str__(self):
        
        return "Camino: extremos ="   " "   str(self.extremos)   ", "   "color = "   self.color
        
    def rotar(self,grados,sentido):
        self.grados = grados
        self.sentido = sentido
        
        for i in range(0,2):
            
            if sentido == "horario":
                if grados == 90:
                    
                    if self.extremos[i] == "N":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "N"
                        
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"
                        
                elif grados == 180:
                    
                    if self.extremos[i] == "N":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "N"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"
                        
                elif grados == 270:
                    
                    if self.extremos[i] == "N":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "N"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"
                        
            elif sentido == "antihorario" :
                
                if grados == 90:
                
                    if self.extremos[i] == "N":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "N"
                    
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"
                        
                elif grados == 180:
                    
                    if self.extremos[i] == "N":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "N"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"
                        
                elif grados == 270:
                    
                    if self.extremos[i] == "N":
                        self.extremos[i] = "E"
                        
                    elif self.extremos[i] == "O":
                        self.extremos[i] = "N"
                        
                    elif self.extremos[i] == "S":
                        self.extremos[i] = "O"
                        
                    elif self.extremos[i] == "E":
                        self.extremos[i] = "S"
                        
                    elif self.extremos[i] == "F":
                        self.extremos[i] = "F"

# Crea tu clase Baldosa
class Baldosa:
    
    def __init__(self,caminos):
        
        self.caminos = caminos
        
        for cantidad in range(len(caminos)):
            cantidad  = 1
            
    def camino(self,fila,col):
        
        self.fila = fila
        self.col = col
        
        return "Baldosa: Num. de Caminos ="   str(self.camino)   "Ubicacion ="   "("  str(self.fila)   ","   str(self.col)   ")"

 

Комментарии:

1. Не могли бы вы, пожалуйста, показать нам свой main.py, особенно линия 12 и ее окрестности?

2. В вашем вопросе недостаточно кода, чтобы ответить на него.

3. Camino имеет rotar метод; Baldosa не имеет. Как Baldosa представляется, обертка вокруг списка Camino объектов, я подозреваю, что вы хотите что-то вроде baldosa.caminos[i].rotar(...) .

Ответ №1:

Вы получаете это сообщение, потому что вы создаете экземпляр класса Baldosa, а затем пытаетесь выполнить rotar метод. Вы определили его только для экземпляров класса Camino, потому что он написан внутри класса Camino. Поэтому либо добавьте rotar метод в класс Baldosa, либо создайте экземпляр класса Camina, т. е.

 camino = Camino()
camino.rotar(270, "horario")