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)
< /code>
Я пытаюсь реализовать бинарное oDDER на 4 бита. Я получаю следующую ошибку. В чем проблема с строкой 22? Я не понимаю, почему есть ошибка вне диапазона. < /P>
Ошибка: < /p>
Traceback (most recent call last):
File "ex9.py", line 85, in
adder(number1,number2,op)
File "ex9.py", line 22, in adder
result[x] = 0
IndexError: list assignment index out of range
Подробнее здесь: https://stackoverflow.com/questions/260 ... -in-python
Traceback (самый последний звонок последний) ошибка в Python ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ошибка PIP: Ошибка: Исключение: Traceback (последний звонок в последний раз):
Anonymous » » в форуме Python - 0 Ответы
- 18 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Ошибка PIP: Ошибка: Исключение: Traceback (последний звонок в последний раз):
Anonymous » » в форуме Python - 0 Ответы
- 7 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Ошибка PIP: Ошибка: Исключение: Traceback (последний звонок в последний раз):
Anonymous » » в форуме Python - 0 Ответы
- 2 Просмотры
-
Последнее сообщение Anonymous
-