Automate Loop based in how many the user want in python

My professor asked me to simulate Boolean Gates and the output is a truth table

To simulate an n=3 Gates you need 3 for loop

for i in range(0,2):
     for j in range(0,2): 
           for k in range(0,2):
                 stuff

To simulate an n=2 Gates you need 2 for loop

for i in range(0,2):
     for j in range(0,2): 
                 stuff

i wanted to have an an n= user input, so there may be a chance where the user input more than 10 is there a way to automate the loop??

if there’s any reference, especially if it uses an oop paradigm, it would be very helpfull Thank you

Answer

It looks like you want to generate all the possible 2**n combinations of values for i,j,k,.... If that is the case, you can use itertools.product to generate them all:

from itertools import product

for p in product(range(2), repeat=3):
    print(p)

Which will produce:

(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

Simply change repeat=n and you are done.

Cheers!