I am trying to print a table of values of a function. If X is 1 then Y would be something like 4, so in a Column all X values and in Y all the solutions. For this. I am using an eval that solves the string equation, the program asks for number of range for X, named as "RANGO". Then it sums x + 1 and calculates the eval, then, again, until the loop is finished within the range. The problem is, the dictionary its created well, but, in the tabulate, it inserts the RAW Data of the Dictionary. So here is how my program looks right now:
import re
from tabulate import tabulate
def funcion():
while True:
global funcion
funcion = input("Introduce una Funcion. ")
if re.match('.[a-zA-Z].' ,funcion) and not re.match("[x1-9]", funcion):
print("Incorrecto, contiene una letra")
elif "x" not in funcion:
print("No contiene la X necesaria en una funcion: ")
else:
rango = int(input("Introduce la cantidad de valores de Y que quieres sacar: "))
if re.match("(\d+)x", funcion):
funciont = re.sub(r'(x)', r'*\1', funcion)
for x in range(rango):
x = x + 1
print(eval(funciont), "= Y", "x = ",x)
else: #CREO LOS DICCIONARIOS
s = {}
r = {}
for x in range(rango): #PARA X EN RANGO ESPECIFICADO,SE SUMA 1
x = x + 1 #SE DEFINE X EN CADA CICLO, MAS UNO
s["{0}".format(x)] = eval(funcion)
r["{0}".format(x)] = x
print(x)
break
#CON DARLE VALOR A X EL EVAL AUTO CAMBIA X POR EL VALOR DE ELLA.
global tabla
tabla = [["Val Y", "Val X"],[s, r]]
print(tabulate(tabla, headers='firstrow', tablefmt='fancy_grid'))
funcion()
As you can see, the tabulate looks like this: TABLE
And I want it to look like this:
╒══════════════╤═══════════════╕
│ X │ y │
╞══════════════╪═══════════════╡
│ 1 │ 6 │
├──────────────┼───────────────┤
│ 2 │ 12 │
├──────────────┼───────────────┤
│ 3 │ 18 │
├──────────────┼───────────────┤
│ 4 │ 24 │
╘══════════════╧═══════════════╛