此代码采用命题函数,例如 x1 | x2 并输出是否可满足。变量使用 x+number (x1,x2,x3) 并使用按位运算符。目前它可以工作,但我想实现它以接受更多变量(x10 等)。我怎样才能做到这一点,以便有 num_variable for 循环,每个循环 x+number 在 0 和 1 之间?
user_input = input("Enter a valid propositional statement: ")
num_variables = int(input("How many variables are there?"))
truth_value = 0
for x1 in range(0, 2):
for x2 in range(0, 2):
for x3 in range(0, 2):
for x4 in range(0, 2):
if eval(user_input):
truth_value = 1
break
if truth_value:
print("The propositional statement is satisfiable")
else:
print("the propositional statement is unsatisfiable")
尝试使用字典来存储变量,尝试使用诸如 f 字符串和连接之类的东西来操纵名称