Writing code with python 3

Asked

Viewed 100 times

0

Good guys are... I am currently working on a project, where in one of the functions the script has to write a certain code that was generated from parameters chosen by the user, but after generating such code and adding it to the target file, I’ve noticed there’s always some error in the identation. To make the same work I have to go to the editor and re-tabulate all the lines, even if the tab is correct, I already tried to change the editor and even use the python itself, but the same error returns.

Follow code to generate:

    def criarHabilidade(self, nome, classe, valorMinimoParaAcertar, danoMinimo, danoMaximo):
        habilidadesCriadas[nome] = classe

        habilidade = '''

def {0}(self, dado, alvo = "sem", estadoDoJogador = "normal"):
    dano = 0
    if estadoDoJogador == "normal":
        if dano >= {1}:
            dano = random.randint({2}, {3})
        else:
            dano = 0
            return "Errou"
        if alvo != "sem":
            resultado = alvo.levarDano(dano)
            if resultado == "Morreu":
                return alvo.nome+": Morre!"
            else:
                return alvo.nome+": -"+str(dano)+" vida. Vida atual: "+str(alvo.vida)
        else:
            return dano
    else:
        return "Impossivel atacar! Jogador morto ou sob efeito de algo..."

'''.format(nome, valorMinimoParaAcertar, danoMinimo, danoMaximo)

Follows the resulting code:

#Habilidades criadas 007689


def oi(self, dado, alvo = "sem", estadoDoJogador = "normal"):
    dano = 0
    if estadoDoJogador == "normal":
        if dano >= 1:
            dano = random.randint(1, 1)
        else:
            dano = 0
            return "Errou"
        if alvo != "sem":
            resultado = alvo.levarDano(dano)
            if resultado == "Morreu":
                return alvo.nome+": Morre!"
            else:
                return alvo.nome+": -"+str(dano)+" vida. Vida atual: "+str(alvo.vida)
        else:
            return dano
    else:
        return "Impossivel atacar! Jogador morto ou sob efeito de algo..."

NOTE: I am using object orientation, and according to the editors the code is correct, however I always have to perform the procedure I have already commented.

1 answer

0


Python has some problems with indentation and tab.

When I use text editors like Atom (and the like) I always configure to always use white spaces in python instead of tabulation. Use a fixed number of whitespace instead of tabulation to solve your problem.

  • Thanks!! As soon as you get home I’ll be there!

Browser other questions tagged

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