-3

Ive defined this python function for some reason its giving me an indentation error for the print line statement present after the:

print '-------------------------------------------------------------'

I could not figure out why.

def seq_type(file_handle):
    is_DNA = True
    is_RNA = True
    is_protein = True
    proteins = 'arndcqeghilkmfpstwyv'
    for line in file_handle:
        line.strip()
        if line[0] == '>': #skip this line since its fasta headear and therefore
            continue       #no sequence at this line

        print line
        if is_RNA == True:
            for char in line:
                print 'Character is: ', char
                if char.lower() == 'a' or char.lower() == 'u' or char.lower() == 'c' or char.lower() == 'g'or char == '\n':
                   is_RNA = True               
                   print is_RNA
                else:
                    is_RNA = False
                    print 'is RNA? ', is_RNA
                    break
    print is_RNA        

    print '-------------------------------------------------------------------'

        print line
        if is_DNA == True:
            for char in line:
                print 'Character is: ', char
                if char.lower() == 'a' or char.lower() == 't' or char.lower() == 'c' or char.lower() == 'g'or char == '\n':
                   is_DNA = True               
                   print is_DNA
                else:
                    is_DNA = False
                    print 'is DNA? ', is_DNA
                    break
    print is_DNA        
  • `print line` is indented, it should not. – Mel Aug 29 '15 at 16:28
  • But there should be an indent after `def seq_type(file_handle)` – Mel Aug 29 '15 at 16:33
  • yes sorry I lost the indentations while copying and pasting the code.... please see the edited post @tmoreau – Mossig Stamboulian Aug 29 '15 at 16:35
  • You forgot to include the exact error message. __Notably, error messages usually include the line that they occur on.__ – Bill Lynch Aug 29 '15 at 16:35
  • You intend all the print to be in the for block, right ? You need to indent them all 2 indentation level. – Mel Aug 29 '15 at 16:37
  • The only thing that pops out to me is that you have a `#comment` in-line with an `if something: ` statement. Although your error does not occur on that line, I've often had errors when doing that (putting a comment on the same line as an `if` statement). Try moving the comment on `if line[0]:` to the next or previous line. (Also note that when using triple-quoted comments - which you haven't - they need to be indented properly because they're not really comments, just strings that don't do anything.) – Demis Aug 29 '15 at 16:37
  • your code is not correct, you should only test for illegal characters not for legal ones. `line.strip()` does nothing do. – Daniel Aug 29 '15 at 16:40

3 Answers3

2

it likely should be like this:

(...)    
    print '-------------------------------------------------------------------'

    print line
    if is_DNA == True:
        for char in line:
            print 'Character is: ', char
            if char.lower() == 'a' or char.lower() == 't' or char.lower() == 'c' or char.lower() == 'g'or char == '\n':
                is_DNA = True               
                print is_DNA
            else:
                is_DNA = False
                print 'is DNA? ', is_DNA
                break
    print is_DNA  

You've edited your post... I amended my answer.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
1

There is unexpected indentation between these two lines:

    print '-------------------------------------------------------------------'

        print line
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

It's because you've indented it wrong. print line is indented after an unindented line, but there's nothing starting a block.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • I lost all indentation while copying pasting the code please see the edited post now @Charlie Martin – Mossig Stamboulian Aug 29 '15 at 16:35
  • ohh yes offcourse... I just couldn't see it. Thank you @Charlie Martin – Mossig Stamboulian Aug 29 '15 at 16:41
  • Python usually ignores blank-space lines doesn't it? So I thought a line with only white-space shouldn't affect indentation. Just tried it on Spyder 2.7 - a blank line after `for:` works fine, as long as the next line with code is indented properly. – Demis Aug 29 '15 at 16:41
  • It does, but that's not the issue. he issue is that the `print '---'` is not indented, and the `print line` is. So Python wants to see `print line` as belonging to an indented block, but there's nothing to indicate the start of the block. I discussed it further here http://stackoverflow.com/questions/356638/how-would-you-parse-indentation-python-style. – Charlie Martin Aug 29 '15 at 17:58