IndentationError: unexpected unindent WHY???
#!/usr/bin/python import sys class Seq: def __init__(self, id, adnseq, colen): self.id = id self.dna = adnseq self.cdnlen = colen self.prot = "" def __str__(self): return ">%s\n%s\n" % (self.id, self.prot) def translate(self, transtable): self.prot = "" for i in range(0,len(self.dna),self.cdnlen): codon = self.dna[i:i+self.cdnlen] aa = transtable[codon] self.prot += aa def parseCommandOptions(cmdargs): tfname = cmdargs[1] sfname = cmdargs[2] return (tfname, sfname) def readTTable(fname): try: ttable = {} cdnlen = -1 tfile = open(fname, "r") for line in tfile: linearr = line.split() codon = linearr[0] cdnlen = len(codon) aa = linearr[1] ttable[codon] = aa tfile.close() return (ttable, cdnlen) def translateSData(sfname, cdnlen, ttable): try: sequences = [] seqf = open(seq_fname, "r") line = seqf.readline() while line: if line[0] == ">": id = line[1:len(line)].strip() seq = "" line = seqf.readline() while line and line[0] != '>': seq += line.strip() line = seqf.readline() sequence = Seq(id, seq, cdnlen) sequence.translate(ttable) sequences.append(sequence) seqf.close() return sequences if __name__ == "__main__": (trans_table_fname, seq_fname) = parseCommandOptions(sys.argv) (transtable, colen) = readTTable(trans_table_fname) seqs = translateSData(seq_fname, colen, transtable) for s in seqs: print s
It says:
def translateSeqData(sfname, cdnlen, ttable): ^ IndentationError: unexpected unindent
WHY? I have checked a thousands times and I can't find the problem. I have only used Tabs and no spaces. Plus, sometimes it asks to define the class. Is that Ok?
The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper.
Just say "no" to tabs. Most editors allow them to be automatically replaced by spaces. The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.
The cause of Indentation Error in Python Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error.
It's because you have:
def readTTable(fname): try:
without a matching except
block after the try:
block. Every try
must have at least one matching except
.
See the Errors and Exceptions section of the Python tutorial.
you didn't complete your try
statement. You need and except
in there too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With