Dziabaduch
Dziabaduch

Reputation: 23

index out of range in while

I need help with one thing, my program sometimes works, sometimes not. It's not working for those sequences of amino acids,

when:

a = 'MEATPGYQDISALEKSVTYISLCFKVANAGSQLYTMAHKLHAVERSLEQTTMEQMDEMEVLELLESMSEVTNEYQNLRKDIREVQQLQRDVSSSIRYQMRSMQQTFHTLKQRIASSQKGRKKPEKGLDGAGVGIHE'
b = 'MRRGFAQSSKWLARNAKANLPTAVGAGIVPAPVKMGRSQYGRLLSLTKSEKWVLTAGIGCLVVSSAITMSVPLFLGKVIDVVFNKSGMDSAAMAKLGEYSVLLFGIFVLGGFANFARVHLFGNAALRIVRSLRSRLYRSMLMQEVGWFDTKGTGELINRLSNDTLMVGTSLSQNVSDGLRSVAMIGVGTGMMIYTSPQLAAVSALVVPAMAGMAIVYGRYVRRITKVELDKYAEIMKFAEERFGNVKTVKTFCREQQEVAAFDGKLDEALQIGYKETRARAIFFGLVRPKGKVLRKIIKICYPLFQTGFCGNFIIISVLYYGGTLVLQDSLTIGALTAFMLYAGYVAISMNGLSNFYSQLNKGIGASERIWEILDRECSIPIDKGVVPLEKPVGEVGFQNVFFTFPTRPESAVLTDFSLNLMPGTTTAVVGRSGSGKTTIALLMLRLYDPQGGTVHLDGIDLRTVNPQWLRNNIGAVSQEPVLFSCSIRENILYGANPGETPSPERLQQVIEDANVSQFTDQLPDGLDTLVGQRGMMLSGGQKQRVAIARALIKNPAILILDEATSALDAVSENLVQNALDNLIQGRTVLTIAHRLSTIRNADQIAVLSDGKIVEQGSYNELMGIQEGVFRELVASQAFGSRN'
a1 = len(a)
b1 = len(b)

and my program looks like

That:

porownanie = True
for i in range(a1):
    for j in range(b1):
        if a[i]==b[j]:
            x1.append(i)
            y1.append(j)
            r=0
            while a[i+r]==b[j+r] and porownanie == True:
                dlugoscir.append(r+1)
                if r==3:
                    i1.append(a[i],a[i+1],a[i+2],a[i+r])
                    j1.append(b[j],b[j+1],b[j+2],b[j+r])
                elif r>3:
                    i1.append(a[i+r])
                    j1.append(b[j+r])
                r=r+1
                porownanie = len(a[0:i+r+1])<a1 and len(b[0:j+r+1])<b1

I have problem with command while, not all times, but with this sequence it shows string index out of range and i haven't got idea why. It should stop, when anything in command while is false.

Thank you for Advance

Upvotes: 0

Views: 73

Answers (2)

sabbahillel
sabbahillel

Reputation: 4425

You need to test that r does not cause the indexes being used to be too large.

You can make the test part of the while

while i+r < a1 and  j+r < b1 and a[i+r] == b[j+r]:

As we can see in the code.

for i in range(a1):
  for j in range(b1):
    if a[i]==b[j]:
        x1.append(i)
        y1.append(j)
        r=0
        while i+r < a1 and  j+r < b1 and a[i+r] == b[j+r]:
            dlugoscir.append(r+1)
            if r==3:
                i1.append(a[i],a[i+1],a[i+2],a[i+r])
                j1.append(b[j],b[j+1],b[j+2],b[j+r])
            elif r>3:
                i1.append(a[i+r])
                j1.append(b[j+r])
            r += 1

Upvotes: 1

Sorawee Porncharoenwase
Sorawee Porncharoenwase

Reputation: 6502

First of all, please don't write porownanie == True. porownanie is a Boolean value itself, so only porownanie is enough

The problem is while a[i+r]==b[j+r] and porownanie == True:. Python evaluates expressions from left to right, so when porownanie is False, it's supposed to exit the loop, but it got error from a[i+r]==b[j+r] before it could exit the loop!

The fix is easy: just switch the order

while porownanie and a[i+r]==b[j+r]:

By the way, this is a simpler version of porownanie = len(a[0:i+r+1])<a1 and len(b[0:j+r+1])<b1 which is also more efficient:

porownanie = i+r+1 < a1 and j+r+1 < b1

because it doesn't need to construct a new list from slicing. Just an easy arithmetic calculation.

You could as well remove porownanie entirely and move the expression to the condition of the while loop:

while i+r+1 < a1 and j+r+1 < b1 and a[i+r]==b[j+r]:
    ...

Upvotes: 0

Related Questions