简体   繁体   中英

Why do I get this error and how to solve it?

I'm doing an OOP exercise and I can't fix this error. If someone could help me and check if I am correct in my code it would be very helpful.

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

You get this message because you create an instance of the class Baldosa and then attempt to execute the rotar method. You only defined it for instances of the class Camino because this is written inside the Camino class. So either add the rotar method to the Baldosa class or create an instance of the Camina class ie

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

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