0

The following function creates a 3 bit adder truth table with Sum and Carrier. Now, I want to use the output of the following function as an array for further operation.

import numpy as np

def truthTable(inputs=3):
    if inputs == 3:
        print("  A B C S Cout ")
        for a in range(0, 2):
            for b in range(0, 2):
                for c in range(0, 2):
                        cout = eval("((a & b) | (a & c) | (b & c))")
                        s = eval("(a ^ b ^ c)")
                        print([str(a) + ", " + str(b) + ", " + str(c) + ", " + str(s) + ", " + str(cout)])

truthTable()

By using "print(np.array([truthTable()]))" command, I didn't get expected array structure. I want it as,

[[0, 0, 0, 0, 0] 
 .............. 
 [1, 1, 1, 1, 1]]
  1. How can I get that array structure from this situation?
  2. Also, how to get rid of '' from the array, when this code execute? (e.g.['1, 1, 1, 1, 1'])

Thank you!

Rupak
  • 35
  • 5
  • `return` something from the function? This way you can use its output as another variable – guin0x Sep 03 '22 at 22:11
  • I think the question was more about building a 2D list than just a print vs return question. Shouldn't have been closed – Sajad Sep 03 '22 at 22:17
  • @Sajad thanks for youre reply. Yes, my question was actually about building a 2d structure. Can you help me out? – Rupak Sep 03 '22 at 22:29
  • instead of printing the list, append the list to a result list: => `result.append([a, b, ..., cout]` – Sajad Sep 03 '22 at 22:32

0 Answers0