0
  def matrixADD(A,B):
    minilist=[]
    Z = []
    a=-1
    b=-1

The condition in while loop below is not being followed for some reason so the index goes out of range. To fix it I had to add nested if statement below to break out of the loop. It is very weird I was wondering why is it not working without the if statement?

    while len(A)!=len(Z):
        x=-1
        y=-1
        a=a+1
        b=b+1   

        if len(minilist)!=0:
            Z.append(minilist)
            if len(A)==len(Z):
                break
        minilist=[]    

        while len(A[a])!=len(minilist):
            for numbers in A[a]:
                x=x+1
                y=y+1            
                answer=(A[a][x]+B[b][y])
                minilist.append(answer)

    return Z

def main():
    #Test matrices
    A = [[2,4], [7,0], [6,3]]
    B = [[3,1], [-1,8], [-3, 3]]
Z=matrixADD(A,B)
    print("A + B:",Z)

main()

Hi guys. I wrote this program and it works just fine but I was having some trouble with 1 thing (indicated above)

Thanks for your input guys :)

9
  • What line is it giving you the exception on? Commented Mar 15, 2014 at 23:57
  • Your code is unreadable. Eliminate unnecessary whitespace and name your variables something descriptive (I should know what A is, for god sakes, and why we want to repeat doing stuff to it until minilist is as long as it! Commented Mar 15, 2014 at 23:57
  • Also it looks like you're iterating through lists using an index instead of for element in iterable. Don't do that in Python. Tell us what you're trying to accomplish and it will work a whole lot better than trying to fix a bad implementation of it Commented Mar 15, 2014 at 23:58
  • @Stendika when i run the debug process it continues after the addition of 3rd mini list i.e. sum of 3rd nested list in A and B to cancel it i had to put in that conditional statement. Commented Mar 16, 2014 at 0:01
  • @adsmith if you look down I had written A. If you had trouble reading the code then I will edit it right away. Commented Mar 16, 2014 at 0:02

1 Answer 1

1

It looks like you expected the loop to end as soon as the element was added that caused the list sizes to match. That's not how loops work.

The while loop condition is only tested at the start of each iteration of the loop. If it stops being true in the middle of the loop, Python won't notice until the start of the next iteration, and only if it's still not true at that point. If you want the loop condition to be tested in the middle of the loop, you need to manually test it with something like your if statement, or you need to reorganize your loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Great explanation dude. I am in my first year comsci class this year and what you just said has not been taught. That is exactly what i expected. Thanks again. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.