为什么会出现此错误以及如何解决它?

2024-09-22 16:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在进行OOP练习,无法修复此错误。如果有人能帮助我,检查我的代码是否正确,这将是非常有帮助的

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) + ")"


Tags: inselfifdefcolcolorelifstr
1条回答
网友
1楼 · 发布于 2024-09-22 16:39:56

之所以收到此消息,是因为您创建了类Baldosa的实例,然后尝试执行rotar方法。您只为Camino类的实例定义了它,因为它是在Camino类内部编写的。 因此,要么将rotar方法添加到Baldosa类,要么创建Camina类的实例,即

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

相关问题 更多 >