为什么我的数组中的值不通过我的全位加法器

时间:2019-05-17 04:16:06

标签: python-3.x

# 8-bit-binary adder

#if you have questions about the code just ask


# arrays and funtions
Array1 = []
Array2 = []
#Input A and Validation
def vaildNumberA():
        a = int(input("Enter your A value:"))
        if (a < 0):
                print("Please Enter A Valid Number For Input A")
        elif (a > 255):
            print("Please Enter A Valid Number For Input A")
        else:
             Array1 = [int(x) for x in list('{0:08b}'.format(a))]
#Input B and Validation
def vaildNumberB():
            b = int(input("Enter your B value:"))
            if (b < 0):
                print("Please Enter A Valid Number For Input B")
            elif (b > 255):
                print("Please Enter A Valid Number For Input B")
            else:
                Array2 = [int(x) for x in list('{0:08b}'.format(b))]


# and gate

# AND Gate
def AND (a,b):
    if (a == 1 and b == 1):
        return 1
    else:
        return 0


# or gate


#OR Gate
def OR(a,b):
    if (a == 1 or b == 1):
       return 1
    else:
        return 0

# XOR GATEE


#XOR Gate   
def XOR (a,b):
    if (a == b):
        return 0
    else: 
        return 1

#carry formula
def carryformula(a,b,c,d):
    return OR(AND(a,b), AND(c,d))

# this is where the calculation should be done

#formula for sum
def calculateSum(Array1,Array2): 
    carry = ""
    sumof = []
    for index, in range(len(Array1)):
        list2 = Array2[index]
        sec_xor_form = XOR(Array1[index],Array2[index])
        sumof.append(XOR(sec_xor_form,carry))
        carry = carryformula(Array1[index],Array2[index],sec_xor_form,carry)  
    return list(reversed(sumof))
calculateSum(Array1,Array2)

def main(Array1,Array2):
    vaildNumberA()
    vaildNumberB()
    while True:
        a = Array1
        b = Array2
        total = calculateSum(list(reversed(Array1)),list(reversed(Array2)))
        print(total)
        quit = input("if want to quit type q: ")

        if quit == 'q':
            break
main(Array1,Array2)
  

在发送中打印0

1 个答案:

答案 0 :(得分:0)

代码中的唯一问题是,一旦执行代码正常工作,您需要从函数中返回Array1Array2并将它们分配在while true循环中。

更新后的代码为

# 8-bit-binary adder

#if you have questions about the code just ask


# arrays and funtions
Array1 = []
Array2 = []
#Input A and Validation
def vaildNumberA():
    Array1 = []
    a = int(input("Enter your A value:"))
    if (a < 0):
            print("Please Enter A Valid Number For Input A")
    elif (a > 255):
        print("Please Enter A Valid Number For Input A")
    else:
         Array1 = [int(x) for x in list('{0:08b}'.format(a))]

    #Return the array 
    return Array1

#Input B and Validation
def vaildNumberB():

    Array2 = [] 
    b = int(input("Enter your B value:"))
    if (b < 0):
        print("Please Enter A Valid Number For Input B")
    elif (b > 255):
        print("Please Enter A Valid Number For Input B")
    else:
        Array2 = [int(x) for x in list('{0:08b}'.format(b))]

    #Return the array
    return Array2

# AND Gate
def AND (a,b):
    if (a == 1 and b == 1):
        return 1
    else:
        return 0
#OR Gate
def OR(a,b):
    if (a == 1 or b == 1):
       return 1
    else:
        return 0

#XOR Gate   
def XOR (a,b):
    if (a == b):
        return 0
    else: 
        return 1

#carry formula
def carryformula(a,b,c,d):
    return OR(AND(a,b), AND(c,d))

# this is where the calculation should be done

#formula for sum
def calculateSum(Array1,Array2): 
    carry = ""
    sumof = []
    for index in range(len(Array1)):
        list2 = Array2[index]
        sec_xor_form = XOR(Array1[index],Array2[index])
        sumof.append(XOR(sec_xor_form,carry))
        carry = carryformula(Array1[index],Array2[index],sec_xor_form,carry)  
    return list(reversed(sumof))

#No need of a main function
while True:

    #Call the function from within the while True loop 
    Array1 = vaildNumberA()
    Array2 = vaildNumberB()
    total = calculateSum(list(reversed(Array1)),list(reversed(Array2)))
    print(total)
    quit = input("if want to quit type q: ")

    if quit == 'q':
        break

输出看起来像

Enter your A value:5
Enter your B value:5
[0, 0, 0, 0, 1, 0, 1, 1]
if want to quit type q: 7
Enter your A value:9
Enter your B value:8
[0, 0, 0, 1, 0, 0, 0, 1]
....