Polynomial Evaluator II
# This program evaluates polynomials. It first asks for the coefficients
# of a polynomial, which must be entered on one line, highest-order first.
# It then requests values of x and will compute the value of the poly for
# each x. It will repeatly ask for x values, unless you the user enters
# a blank line. It that case, it will ask for another polynomial. If the
# user types quit for either input, the program immediately exits.
# Need some string services, and some standard system services.
import string, polynomial
# Run until some kind of endfile.
# Repeat until an exception or quit gets us out.
# Read a poly until it works. An EOF will except out of the
poly = polynomial.read('Enter a polynomial coefficients: ')
print('Conversion failed. Please try again.')
# Read and evaluate x values until the user types a blank line.
# Again, and EOF will except out of the pgm.
resp = input('Enter x value or blank line: ')
if resp == 'quit': raise EOFError
if not resp: break
x = int(resp)
print("That doesn't look like an integer. Please try again.")
print('p(x) =', polynomial.srep(poly))
print('p(' + str(x) + ') =', polynomial.eval(x, poly))
except (EOFError, KeyboardInterrupt):
# Exit without error for EOF or ^C. Print a blank line to clear after
# any prompt.
This uses the Polynomial module to recreate the
polymomial evaluator program.