Finite looping

Asked

Viewed 63 times

-1

I am transcribing from Java to Python a code to solve a Sudoku 4/4.

When I test the code it doesn’t run and runs endlessly, apparently in one of the methods of the Artist class. Can someone help me find the mistake?

import random

class Artista:

    __caracteristicas = list(range(0,4))

    def __init__(self):
        self.gerar_caracteristicas_artista()

    def gerar_caracteristicas_artista(self):
        aux = random.randrange(0,4) + 1
        for i, v in enumerate(self.__caracteristicas):
            while self.__verifica_repeticao(aux):
                aux = random.randrange(0,4) + 1
            self.__caracteristicas[i] = aux

    def __verifica_repeticao(self,valor):
        for i, v in enumerate(self.__caracteristicas):
            if valor == v:
                return True
        return False

    def imprime_caracteristicas(self):
        print(self.__caracteristicas)

    def get_caracteristicas(self):
        return self.__caracteristicas from sudoku import Artista class Grupo:
    __artistas = list(range(0,4))

    def __init__(self):
        artista1 = Artista.Artista()
        artista2 = Artista.Artista()
        artista3 = Artista.Artista()
        artista4 = Artista.Artista()
        self.__artistas.clear()
        self.__artistas = [artista1,artista2,artista3,artista4]

    def imprime_artistas(self):
        for i in self.__artistas:
            self.__artistas[i].imprime_caracteristicas()
            print('\n') from sudoku import Grupo

class Sudoku:
    grupo= Grupo.Grupo()
    grupo.imprime_artistas()
  • has some things in its code that ñ recognize how it works '''def get_characteristics(self): Return self. __characteristics from sudoku import Artist class Group: __artists = list(range(0,4))''' how this part works?

1 answer

0

I don’t do much with Python, but I did find some things kind of out of place in that code. As you are transcribing from Java, you are trying to do some things that are allowed in Java but do not work in Python.

In Python, if you define a method, and later define that same method again, its previous definition will be 'overwritten', Python will interpret only the last method definition.

In your code, when defining __init__(self) a second time, you deleted the first definition, therefore, the command self.gerar_caracteristicas_artista() will not be executed. To solve this, just define once the method, with everything necessary within.

In python, to create an instance of a class, it is not necessary to do Artist.Artist(), just Artist() (Although it makes no sense to me that you instantiate a class within the class itself, so I understand that it would create something recursive, creating an instance within another and within another, and so on, I believe that this is the part where you’re looping).

In his method get_caracteristicas(self), as in the imprime_artistas(self), I think it’s something else that works in Java, but it’s not quite like that in Python. If you want to import a class from a module, set it at the beginning of the code, just like you did with Random.

That said, I think these are the main errors in this code (although I’m still trying to understand the logic of the method gerar_caracteristicas_artista(self), for me, either he loops, or he will always produce the same sequence, but anyway).

Better know a little more of the Python syntax before transcribing, if not many errors will still appear in this code. There are many good references, although most in English. Looking a little on youtube, you will find several tutorials for object orientation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.