Since the particular variable, such as x
, does not matter, just have the user enter a sequence of numbers, which will be the coefficients of the polynomial. Have the user enter the constant coefficient, then the linear coefficient, then the quadratic coefficient, and so on. If these are done as separate inputs you would need a stop signal, one that is not a number. If you want it all as one input, the user could put spaces or commas or something similar between the coefficients, and no stop signal would be needed.
You then place the coefficients into a list: if the input was one line, just use the split()
method. Any operations on polynomials, such as substitution into the variable, can be done with this list. Substitution, for example, can be done easily and efficiently with Horner's method. There are simple algorithms for addition, subtraction, multiplication, division, differentiation, and so on. Let me know if you need more details on how to implement any of those algorithms in Python.
Python's eval()
function is very dangerous, so I highly recommend that you do not use that, despite its simplicity.