python - how to add inputs to this piece of code? -


if use following code introducing parameters manually, works:

def evaluatepoly(poly, x):     result = 0     in range(len(poly)):         result += poly[i] * x **     return float(result)  >>> evaluatepoly([1,2,2],2) 13 

i asked introduce coefficients, without brackets, , value want evaluate polynomial equation. this:

poly=(raw_input('enter list of coefficients polynomial equation: ')) x=int(raw_input('enter value want evaluate polynomial equation: '))  print(evaluatepoly(poly, x)) 

but if try that, python gives me error:

typeerror: unsupported operand type(s) +=: 'int' , 'str' 

how can it?

thanks

raw_input return string. can process string list of values so:

coeffs = raw_input('enter list of coefficients polynomial equation: ') # string poly = coeffs.split() # split string based on whitespace poly = map(int, poly) # convert each element integer using int(...) 

if want accept floats, use float instead of int, , if want split on comma, use coeffs.split(",") instead of coeffs.split().

example

>>> x=int(raw_input('enter value want evaluate polynomial equation: ')) enter value want evaluate polynomial equation: 2 >>> coeffs = raw_input('enter list of coefficients polynomial equation: ') enter list of coefficients polynomial equation: 3 2 5 >>> poly = coeffs.split() >>> poly = map(int, poly) >>> print(evaluatepoly(poly, x)) 27.0 >>> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -