pythontraceback

Traceback (Most recent call last) error in python


def adder(l1,l2,op):

    if(op == 0):

        carry = 0
        result = []
        l1.reverse()
        l2.reverse()

        for x in range(0, 4):

            sum = l1[x] + l2[x] + carry

            if(sum == 0):
                result[x] = 0
                carry = 0
            elif(sum == 1):
                result[x] = 1
                carry = 0
            elif(sum == 2):
                result[x] = 0
                carry = 1
            elif(sum == 3):
                result[x] = 1
                carry = 1

            if(x == 2):
                carry_in = carry
            if(x == 3):
                carry_out = carry

    result.reverse()

    overflow = carry_in ^ carry_out
    sign = result[3]
    zero = not(result[0] | result[1] | result[2] | result[3]) 
    sign_of_true_result = overflow ^ sign

    print result,carry,overflow,sign,zero,sign_of_true_result


number1 = []
number2 = []
print "Enter the bits of number 1 one by one: "

x = 0
while(x < 4):

    digit_1 = raw_input()

    if(digit_1 != '0' and digit_1 != '1'):
        print "Please enter either 0 or 1"
        continue
    else:
        x = x + 1
        number1.append(int(digit_1))

print "Enter the bits of number 2 one by one: "

y = 0       
while(y < 4):

    digit_2 = raw_input()

    if(digit_2 != '0' and digit_2 != '1'):
        print "Please enter either 0 or 1"
        continue
    else:
        y = y + 1
        number2.append(int(digit_2))

op = int(raw_input("Press 0 for addition or 1 for substraction (Op): "))

if __name__ == '__main__':
    adder(number1,number2,op)

I am trying to implement binary adder for 4 bits. I get the following error. What's the problem with line 22 ? I don't understand why there is an out of range error.

Error:

Traceback (most recent call last):
  File "ex9.py", line 85, in <module>
    adder(number1,number2,op)
  File "ex9.py", line 22, in adder
    result[x] = 0
IndexError: list assignment index out of range

Solution

  • You start off with this:

    result = []
    

    So result has no values.

    The first time through the loop, x is 0, and you try to reassign result[0] to either 0 or 1. But there is no result[0]. So you get an IndexError.

    You can see the same thing in a much simpler example:

    >>> result = []
    >>> result[0] = 0
    IndexError: list assignment index out of range
    

    You either need to reorganize your code so that you append or prepend the bits as you get them, or you need to pre-fill result with, e.g., [None, None, None, None] or [0, 0, 0, 0] or something else so that result[0] makes sense.


    I am more interested in the first error which occurs at line 85.

    You don't really have an error at line 85. Notice that the traceback shows "most recent call last":

    Traceback (most recent call last):
      File "ex9.py", line 85, in <module>
        adder(number1,number2,op)
      File "ex9.py", line 22, in adder
        result[x] = 0
    IndexError: list assignment index out of range
    

    The result[x] = 0 at line 22, in adder, raised an IndexError because x is 0 and result is empty.

    The adder(number1,number2,op) at line 85, at the top level of the module, raised an IndexError because it called a function that raised an IndexError and didn't have a try:/except IndexError: (or some superclass of IndexError) around that call.

    And, because that was at the top level, there's no place higher up in your code that it could be handled, so the interpreter handles it by printing out the traceback and exiting.