真相Table。以所需格式打印每行每个真值的总和
Truth Table. Printing the sum of each True value for each row in the desired format
我能够对每行每个 True 语句的值求和,但我无法将其以列格式打印在结果旁边。
`res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format`
这部分代码根据用户输入计算我的逻辑表达式,并根据需要以列格式打印 True/False 结果。但是,我不知道如何以相同的格式显示每行真值的总和。在这段代码下面,我总结了每一行的真相 table,但只能让它打印在我的结果的底部。以下是我对每一行的总结。
`try:
res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format
truth_sum = [] # I create this list
for row in truth_table: # I parse the each row in the truth table
truth_sum.append(sum(row)) # and sum up each True statement
except:
print("Wrong Expression")
print(*truth_sum, sep="\n") # This line is my problem. I can't figure how to print this next to each result
print()`
如果我将 print(*truth_sum, sep="\n")
放在 except:
上方,它会在每个 Truth Table 行之后打印出来。我怎样才能在逻辑表达式列旁边打印出总和列表?这是我的完整代码,以便更好地理解。
`import itertools
truth_table = 0
val = 0
exp = ''
p1 = 0
p2 = 0
p3 = 0
p4 = 0
p5 = 0
p6 = 0
pos = 0
# This function takes care of taking users input
def get_input():
global val
while True:
try:
val = int(input("Please Enter the number of parameters, 1 - 5: "))
if val < 1 or val > 5:
print(f"Enter a value [0,6)")
else:
break
except ValueError:
print(f"Only Numbers are Allowed, Try Again..")
# This function takes care of the Truth Table header
def table_header():
print("========================TRUTH TABLE===============================")
print("p1\t\t\tp2\t\t\tp3\t\t\tp4\t\t\tp5")
print("*" * 20 * val)
# This is the Main method
def main():
# Global Variables
global val
global truth_table
global exp
global p1
global p2
global p3
global p4
global p5
global pos
# Call the userInput Function
get_input()
# Creating the truth table
truth_table = list(itertools.product([True, False], repeat=val))
exp = input("Enter a Logical Statement i.e [(p1 and p2) or p3]: [p1==p2==p3==...Pn:]:").lower()
# printing Table Layout
table_header()
for par in truth_table:
pos = 0
if val == 1:
p1 = par[0]
elif val == 2:
p1, p2 = par[0], par[1]
elif val == 3:
p1, p2, p3 = par[0], par[1], par[2]
elif val == 4:
p1, p2, p3, p4 = par[0], par[1], par[2], par[3]
else:
p1, p2, p3, p4, p5 = par[0], par[1], par[2], par[3], par[4]
pos = 0
while pos < val:
print(par[pos], end='\t\t')
pos = pos + 1
try:
res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format
truth_sum = [] # I create this list
for row in truth_table: # I parse the each row in the truth table
truth_sum.append(sum(row)) # and sum up each True statement
except:
print("Wrong Expression")
print(*truth_sum, sep="\n") # This line is my problem. I can't figure how to print this next to
each result
print()
if __name__ == "__main__":
main()`
这是我的输出示例。
`Please Enter the number of parameters, 1 - 5: 2
Enter a Logical Statement i.e [(p1 and p2) or p3]: [p1==p2==p3==...Pn:]:p1 and p2
========================TRUTH TABLE===============================
p1 p2 p3 p4 p5
****************************************
True True True
True False False
False True False
False False False
2
1
1
0`
如何让每行真值的总和列表显示在逻辑表达式结果旁边而不是底部?
我在此处编写了我为此任务编写的代码版本,避免了 eval() 方法(正如 Chris Doyle 所建议的)并采用了在打印任何内容之前将数据保存在 RAM 中的主要思想.这样你就可以随心所欲地打印了。
下面的代码是"minimized"为了看懂。因此有些东西不包括在内(例如 table header)。无论如何,您可以轻松添加它。
import itertools
# Hard-coded expression to evaluate the parameters of the truth table
EXPRESSION = lambda array: array[0] and array[1]
PARAMETERS_NUMBER = 2
# function to count the total number of true in a row
TRUE_COUNTER = lambda array: sum([int(x==True) for x in array])
truth_table = list(itertools.product([True, False], repeat=PARAMETERS_NUMBER))
# array with the result of the truth table for each row
results = [EXPRESSION(row) for row in truth_table]
# array with the number of true for each parameter row
true_numbers = [TRUE_COUNTER(row) for row in truth_table]
# print the table
for row, result, true_number in zip(truth_table, results, true_numbers):
print(row, result, true_number, "\n")
让我知道它是否对您的任务足够清晰和有用。
我能够对每行每个 True 语句的值求和,但我无法将其以列格式打印在结果旁边。
`res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format`
这部分代码根据用户输入计算我的逻辑表达式,并根据需要以列格式打印 True/False 结果。但是,我不知道如何以相同的格式显示每行真值的总和。在这段代码下面,我总结了每一行的真相 table,但只能让它打印在我的结果的底部。以下是我对每一行的总结。
`try:
res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format
truth_sum = [] # I create this list
for row in truth_table: # I parse the each row in the truth table
truth_sum.append(sum(row)) # and sum up each True statement
except:
print("Wrong Expression")
print(*truth_sum, sep="\n") # This line is my problem. I can't figure how to print this next to each result
print()`
如果我将 print(*truth_sum, sep="\n")
放在 except:
上方,它会在每个 Truth Table 行之后打印出来。我怎样才能在逻辑表达式列旁边打印出总和列表?这是我的完整代码,以便更好地理解。
`import itertools
truth_table = 0
val = 0
exp = ''
p1 = 0
p2 = 0
p3 = 0
p4 = 0
p5 = 0
p6 = 0
pos = 0
# This function takes care of taking users input
def get_input():
global val
while True:
try:
val = int(input("Please Enter the number of parameters, 1 - 5: "))
if val < 1 or val > 5:
print(f"Enter a value [0,6)")
else:
break
except ValueError:
print(f"Only Numbers are Allowed, Try Again..")
# This function takes care of the Truth Table header
def table_header():
print("========================TRUTH TABLE===============================")
print("p1\t\t\tp2\t\t\tp3\t\t\tp4\t\t\tp5")
print("*" * 20 * val)
# This is the Main method
def main():
# Global Variables
global val
global truth_table
global exp
global p1
global p2
global p3
global p4
global p5
global pos
# Call the userInput Function
get_input()
# Creating the truth table
truth_table = list(itertools.product([True, False], repeat=val))
exp = input("Enter a Logical Statement i.e [(p1 and p2) or p3]: [p1==p2==p3==...Pn:]:").lower()
# printing Table Layout
table_header()
for par in truth_table:
pos = 0
if val == 1:
p1 = par[0]
elif val == 2:
p1, p2 = par[0], par[1]
elif val == 3:
p1, p2, p3 = par[0], par[1], par[2]
elif val == 4:
p1, p2, p3, p4 = par[0], par[1], par[2], par[3]
else:
p1, p2, p3, p4, p5 = par[0], par[1], par[2], par[3], par[4]
pos = 0
while pos < val:
print(par[pos], end='\t\t')
pos = pos + 1
try:
res = eval(exp) # evaluate the logical input from the user
print(res) # this prints the result of the logical expression in a column format
truth_sum = [] # I create this list
for row in truth_table: # I parse the each row in the truth table
truth_sum.append(sum(row)) # and sum up each True statement
except:
print("Wrong Expression")
print(*truth_sum, sep="\n") # This line is my problem. I can't figure how to print this next to
each result
print()
if __name__ == "__main__":
main()`
这是我的输出示例。
`Please Enter the number of parameters, 1 - 5: 2
Enter a Logical Statement i.e [(p1 and p2) or p3]: [p1==p2==p3==...Pn:]:p1 and p2
========================TRUTH TABLE===============================
p1 p2 p3 p4 p5
****************************************
True True True
True False False
False True False
False False False
2
1
1
0`
如何让每行真值的总和列表显示在逻辑表达式结果旁边而不是底部?
我在此处编写了我为此任务编写的代码版本,避免了 eval() 方法(正如 Chris Doyle 所建议的)并采用了在打印任何内容之前将数据保存在 RAM 中的主要思想.这样你就可以随心所欲地打印了。
下面的代码是"minimized"为了看懂。因此有些东西不包括在内(例如 table header)。无论如何,您可以轻松添加它。
import itertools
# Hard-coded expression to evaluate the parameters of the truth table
EXPRESSION = lambda array: array[0] and array[1]
PARAMETERS_NUMBER = 2
# function to count the total number of true in a row
TRUE_COUNTER = lambda array: sum([int(x==True) for x in array])
truth_table = list(itertools.product([True, False], repeat=PARAMETERS_NUMBER))
# array with the result of the truth table for each row
results = [EXPRESSION(row) for row in truth_table]
# array with the number of true for each parameter row
true_numbers = [TRUE_COUNTER(row) for row in truth_table]
# print the table
for row, result, true_number in zip(truth_table, results, true_numbers):
print(row, result, true_number, "\n")
让我知道它是否对您的任务足够清晰和有用。