SweetJD14 SweetJD14 - 7 months ago 17
Python Question

Python 2.7 Nested If-statement won't print string literal after printing array position

I have a program that runs through a list of Amino Acid sequences for a protein in Influenza A that I'm analyzing and finds amino acid at position 627 in each amino acid sequence in my FASTA file for each protein.

My code works like this

with open(file, "r" ) as source:
for heading_and_lines in group_by_heading( source ):
heading= heading_and_lines[0]
lines= heading_and_lines[1:]
lines = ''.join(lines)
if lines[627-1] == 'K':
print "---------------MUTATION BELOW--------------"
print heading
print lines[627-1]
#print "-------------------------------------------"

print "end of file"


But my code does not work like this

with open(file, "r" ) as source:
for heading_and_lines in group_by_heading( source ):
heading= heading_and_lines[0]
lines= heading_and_lines[1:]
lines = ''.join(lines)
if lines[627-1] == 'K':
print "---------------MUTATION BELOW--------------"
print heading
print lines[627-1]
print "-------------------------------------------"

print "end of file"


For some reason printing the line below the mutation that I found returns an error, and unexpected indent. See I want the line beneath each mutation, just like the line on top, so I can clearly organize the mutations. Any reason why you think the printing statement below doesn't work?

Sorry if this sounded confusion, and as always thanks for your time.

Answer

Your indentation is wrong, indent your code like this

with open(file, "r" ) as source:
    for heading_and_lines in group_by_heading( source ):
        heading= heading_and_lines[0]
        lines= heading_and_lines[1:]
        lines = ''.join(lines)

        if lines[627-1] == 'K':
            print "---------------MUTATION BELOW--------------"
            print heading
            print lines[627-1]
            print "-------------------------------------------"