简体   繁体   中英

How to join/amend a string to the name of a variable so that it is recognized as another variable using a loop? python troubles

So i am a noob in python and i don't usually ask a question unless i have searched exhaustively or tried several workarounds.I'm Creating an 8bit binary adder, by simulating the logic of various circuit gates.....

Basically i want to take each element in the two lists and feed them into a function that loops 8 times. (8 bits)

Creating two strings. (int can't start with a 0)

example1 = '00001001'
example2 = '11011100'

Assigning the slices to separate strings. One character each.

a1 = example1[7:8]
a2 = example1[6:7]
a3 = example1[5:6]
a4 = example1[4:5]
a5 = example1[3:4]
a6 = example1[2:3]
a7 = example1[1:2]
a8 = example1[0:1]

b1 = example2[7:8]
b2 = example2[6:7]
b3 = example2[5:6]
b4 = example2[4:5]
b5 = example2[3:4]
b6 = example2[2:3]
b7 = example2[1:2]
b8 = example2[0:1]

Adding those slices to lists and converting them into an int.

aToInt = [int(a1),int(a2),int(a3),int(a4),int(a5),int(a6),int(a7),int(a8)]
bToInt = [int(b1),int(b2),int(b3),int(b4),int(b5),int(b6),int(b7),int(b8)]

Main function. Takes two inputs. One from each list... (aToInt and bToInt) (a,b)(a1,b1,a2,b2,a3,b3.....)

def main(a,b): 
    for onebit in range(len(aToInt)):
        a = a{i++} ???

a and b need to change to a1,b1,a2,b2,a3,b3... ++ each interation of the loop..

       ##something like this maybe ("{a++}")?
       XOR1OUT = XOR(a{++},b{++})  
       print(" 1 XOR: ", XOR1OUT)

       AND1OUT = AND(a,b)
       print(" 1 AND: ",AND1OUT)

       AND2OUT = AND(XOR1OUT,c0)
       print(" 2 ANDL ", AND2OUT)

       CARRYOROUT = OR(AND1OUT,AND2OUT)
       print(" CARRY: ", CARRYOROUT)

       XOR2OUT = XOR(XOR1OUT,c0)
       print("final value: ", XOR2OUT)

main()

Other functions that also take two inputs...

def OR(a,b):
    if a or b is 1:
        OR11 = 1
        return(OR11)
    else:
        OR10 = 0
        return(OR10)

def XOR(a,b):
    if a == b:
        XOR10 = 0
        return(XOR10)
    else:
        XOR11 = 1
        return(XOR11)

def AND(a,b):
    if a == 1 and b == 1:
        AND11 = 1
        return(AND11)
    elif a and b == 0:
        return(0)
    else:
        return(0)

Any suggestions and recommendations much appreciated.

=============================EDIT==================== SPECIFICS

So i have a list and i want to loop through that list

list = [a1,b1,a2,b2,a3,b3....a8,b8]


def main():

=code goes here for loop that changes a and b according to the list? (a......,b.....)?

   1st interation...
   dough = cookie(a1,b1)

   2nd interation...
   bread = bagel(a2,b2)


   2nd interation...
   oil = eggs(a3,a3)

   for 8 interations....


def cookie(a,b):
   if some code
   else some code


def bagel(a,b):
   if some code
   else some code


def eggs(a,b):
   if some code
   else some code

So i don't know what to call it but i want to be able to maybe map a,b onto a1,b1.... respectively.

I think you are looking for zip() function

example1 = '00001001'
example2 = '11011100'

for a,b in zip(example1, example2):
    print a, b
    # some_function(int(a), int(b))

output:
0 1
0 1
0 0
0 1
1 1
0 1
0 0
1 0

If you have two lists of numbers:

a_values = [1, 2, 3, 4]
b_values = [10, 20, 30, 40]

and a function:

def func(a, b):
    return a + b

you can use zip to iterate over both of them:

for a, b in zip(a_values, b_values):
    print(func(a, b))

prints:

11
22
33
44

I think your adder is implemented wrong, but you want something like this after you fix it.

example_a = '00001001'
example_b = '11011100'
a_to_int = [int(char) for char in example_a] # == [0, 0, 0, 0, 1, 0, 0, 1]
b_to_int = [int(char) for char in example_b]

def OR(a, b):
    return a | b

def XOR(a, b):
    return a ^ b

def AND(a, b):
    return a & b

def main(a, b):
    c0 = 0
    result = []
    for bit_a, bit_b in zip(a, b):
        xor1out = XOR(bit_a, bit_b)
        print("a XOR b:", xor1out)

        and1out = AND(bit_a, bit_b)
        print("a AND b:", and1out)

        and2out = AND(xor1out, c0)
        print("(a XOR b) AND c:", and2out)

        carryorout = OR(and1out, and2out)
        print("(a AND b) AND ((a XOR b) AND c):", carryorout)

        xor2out = XOR(xor1out, c0)
        print("(a XOR b) XOR ((a AND b) AND ((a XOR b) AND c))):", xor2out)

        c0 = carryorout
        result.append(xor2out)

    return ''.join(str(bit) for bit in result)

Call main(a_to_int, b_to_int) and see what it does.

Note the following salient points:

  • Use list comprehensions; don't type things out 8 times.
  • Python has native bitwise operations for AND, OR, XOR, and NOT.
  • zip pairs up two iterables, and so the for loop iterates over corresponding bits of a and b . But it does so from left to right, which is probably not what you want.
  • return is not a function; it is conventional not to enclose a returned value in parentheses.
  • a or b is 1 does not mean a is 1 or b is 1 , but rather a or (b is 1) .
  • Do not test for integer equality using is . Use == .
  • a{i++} is not valid Python, and does not resemble any valid Python. I would tell you what to replace it with, but I don't even know what you want it to mean.
  • Python has native binary literals, written like so: 0b00001001 , which equals 9 .

In short, what you actually want (a working full-adder) is this:

example_a = '00001001'
example_b = '11011100'

to_ints = lambda s: [int(char) for char in s]

def main(a: str, b: str) -> str:
    c = 0
    result = []
    for bit_a, bit_b in reversed(list(zip(to_ints(a), to_ints(b)))):

        s = (bit_a ^ bit_b) ^ c
        c_out = ((bit_a ^ bit_b) & c) | (bit_a & bit_b)

        result.append(s)
        c = c_out

    return ''.join(str(bit) for bit in result)[::-1]

Now, main(example_a, example_b) == '11100101' , as you would expect.

I believe you're looking for something like this:

a = map(int, '00001001')
b = map(int, '11011100')
#             11010011

def xor(x, y):
    return (x or y) and not (x and y)

carry = 0
bits = []
for a_i, b_i in zip(a, b):
    bit = xor(a_i, b_i)
    new_carry = (bit and carry) or (a_i and b_i)
    bit = xor(bit, carry)
    carry = new_carry
    bits.append(bit)

print(''.join('{:d}'.format(b) for b in bits))

where the lowest bits of a and b come first , as your code seemed to assume. The output of the above is 11010011 , which is 00001001 + 11011100 (again, lowest bits are first).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM