IndentationError: unexpected unindent WHY?

Question:

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 ">%sn%sn" % (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?

Asked By: John

||

Answers:

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.

Answered By: agf

you didn’t complete your try statement. You need and except in there too.

Answered By: Jeff

This error could actually be in the code preceding where the error is reported. See the For example, if you have a syntax error as below, you’ll get the indentation error. The syntax error is actually next to the “except” because it should contain a “:” right after it.

try:
    #do something
except
    print 'error/exception'


def printError(e):
    print e

If you change “except” above to “except:”, the error will go away.

Good luck.

Answered By: MaxPython

@MaxPython The answer below is missing syntax ":"

    try:
       #do something
    except:
      # print 'error/exception'


    def printError(e):
        print e
Answered By: Josh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.