0

This is an example excerpt from a bigger code, I have no idea why is this not properly idented

Code example :

if True:
    for i in range(1, 10):
        print(i)
    print("Inside the if")
    print("But outside the loop")

Actual code :

def mkfolder(self):
    path = self.edit_dir.text()
    prefix,fromn,ton,formatn
    if not os.path.isdir(path):
        return self.showerror("Erro de parâmetro", "Caminho para criar as pastas é inválido ou não existe\nCód.: 0052")
    if not self.chk_prefix.isChecked():
        prefix = ""
    else:
        prefix = self.edit_prefix.text()    
    if self.chk_count.isChecked():
        fromn = self.spin_from.getValue()
        ton = self.spin_to.getValue()
        formatn = self.spin_format.getValue()
        if ton <= fromn:
            return self.showerror("Erro de parâmetro", "Não é possível fazer uma contagem regressiva\nCód.: 0010")
        if len(str(abs(ton))) < formatn:
            return self.showerror("Erro de parâmetro", "Quantidade de dígitos é insuficiente\nCód.: 0012")
        strFormat = "{0:0>"+ str(formatn) +"}"
        msg = self.askquestion("Aviso", str(ton - fromn) + " pastas\n Em " + path + "\n De '" + prefix + strFormat.format(fromn) + "'\na '"+ pref+ strFormat.format(ton) +"'\n\nConfirma?")
        if msg==22:

            for i in range(fromn, ton+1):
                os.makedirs(path + "/"+ prefix + strFormat.format(i))           
            self.tomain()
            self.mkclean()
Mojimi
  • 2,561
  • 9
  • 52
  • 116

3 Answers3

0

It is improperly indented because you're mixing tabs and spaces. Don't do that. Python doesn't like it when you do that. Every time you do that, a puppy cries

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • It seems that my code is full of those after I switched IDEs, any quick solution? – Mojimi Oct 13 '16 at 21:16
  • @Mojimi SublimeText3 should be able to handle that. http://stackoverflow.com/questions/22529265/sublime-text-3-convert-spaces-to-tabs – OneCricketeer Oct 13 '16 at 21:19
  • `find-replace`. Find a tab (`'\t'`), replace with four spaces. Alternatively, run this on the commandline: `python -c 'import sys; with open(sys.argv[-1]) as infile: contents = infile.read().replace('\t', ' '); with open(sys.argv[-1], 'w') as outfile: outfile.write(contents) path/to/file` – inspectorG4dget Oct 13 '16 at 21:19
0

Chances are, you've got inconsistent tabs and spaces. Some text editors have special "Tab Characters" that add the spacing, while others simply convert a tab into multiple "Space Characters". I would recommend looking into how to show non-printing characters in your text editor to see which lines are spaced and which are tabbed.

Matthew Fournier
  • 1,077
  • 2
  • 17
  • 32
0

Might be this tool come in handy to check Indentation in Python

https://pythoniter.appspot.com/

Additional: Make your code look professional

http://pep8online.com/

Dinesh Sunny
  • 4,663
  • 3
  • 30
  • 29