Random truth table with pythontex

Added by Paco Riviere over 2 years ago

With the command building any 2 variable truth table (https://project.auto-multiple-choice.net/boards/2/topics/11192?page=1&r=11222):

def taulaveritat2(hd):
    print(r"\begin{tabular}{c c|c}")
    print(r"$A$ & $B$ &  %s \\ \hline" % hd)
    for a in range(0, 2):
        for b in range(0, 2):
            print(r"%d & %d & %d \\" % (a, b, lofu(a,b)))

The idea is:
1 We build two Python lists, one with all the operators and another with their names.
2. We choose a random i in the lists range
3. We build the table with element i from the first list
4. We ask the name of the table. The answer is the element i in the second list.
5. We need some incorrect choices

The code I am testing is:

# step 1
operador = ["a or b", "a and b", "not (a or b)", "not (a and b)", "a ^ b", "not(a^b)"]
nom = ["O", "I", "NO-O", "NO-I", "XOR", "XNOR"]

# step 2
import numpy as np
# just for testing:
print (i, operador[i], nom[i])
% Also a test:

% step 3
Question: What is the function of the following truth table?
def lofu(a,b):
    return a or b ### This fails with 'return operador[i]' ###
We asking \pyc{print(operador[i])}


% step 4
The correct answer is

% step 5
Wrong values % Cannot use +5 here as there would be two righr answers

It works as expected, except the return statement. As if we use:
return a or be it works
but with
return operador[i] Which is what we need, it fails.

Using pysub also fails:
def lofu(a,b):
return !{operador[i]}

What is wrong with the return statement?