[Tutor] range
Julieta Rangel
julieta_rangel@hotmail.com
Thu, 03 May 2001 18:27:25 -0500
I have the following program but when I run it it only runs half-ways. It
displays a message of out of range. Can you tell me what I'm doing wrong?
class Poly:
def __init__ ( self, v='x' , c = [0]):
"""__init__():
Initializes a polynomial
Default variable is x
Default polynomial is zero"""
self.var = v
self.coef = c
self.deg = len(c)-1
self.length = len(c)
def __str__ (self):
"""__str__():
Converts a polynomial into a string"""
x = `self.coef[0]`
for i in range (1,self.deg+1):
x = x + "+" +`self.coef[i]` + self.var + "^" + `i`
return x
def integrate(self):
"""Input: an instance of a polynommial
Output: a polynomial that is the integral of the input
polynomial
Side Effects: None"""
coef=[0]
self.deg= self.deg +1
for i in range(0,self.deg):
coef.append(self.coef[i]/float(i+1))
self.coef= coef
def Derivative(self):
"""Input: an instance of a polynomial
Output: a polynomial that is the derivative of the input polynomial
side Effects: None"""
coef=[1]
self.deg= self.deg +1
if self.deg==0:
coef=[0]
else:
for i in range(0,self.deg-1):
coef.append((i+1)*self.coef[i+1])
self.coef= coef
degree = input('Enter the degree of your polynomial')
coef=[]
for i in range (0,degree + 1):
coefficient = input('Enter the coefficient for x^ %i ? ' %i)
coef.append(coefficient)
s= Poly (c = coef)
m= Poly (c = coef)
s.integrate()
m.Derivative()
print "the integral of your polynomial is:"
print "c +",s
print "the derivative of your polynomial is:",m
Julieta
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com