I am trying (as a newb Pythoneer) to translate an old homework assignment from Java to Python. I am getting a TypeError when I try to iterate through a list. It is claiming that I am not using an integer value for my index, but I can't see how I am not. I've probably included too much code, but just in case the error isn't where I think it is (TypeError is on very last line):
def power(x, y):
if isinstance(y, int):
solution = x
if y == 0:
return 1
else:
for i in range(1,y):
solution = solution*x
return solution
else:
raise TypeError("Power: Non-Integer power argument")
def factorial(x):
if isinstance(x, int) and x >= 0:
if x == 1 or x == 0:
return 1
else:
solution = int(x)
while x > 1:
solution = solution*(x-1)
x -= 1
return solution
else:
raise TypeError("Factorial: argument must be a positive integer")
def abs(x):
if x < 0:
x = -x
return x
# Calculates the coefficients of the
# Taylor series of Sin(x).
# center argument must be 0, pi/4, pi, or 3pi/2
def coef_calc(center):
coef = [1]*32
i = int(0)
c_temp = 1 # temporary holding place for calculated coefficient
if center in [0, PI/4, PI, 3*PI/2]:
# Mclauren Series (center = 0)
if center == 0:
while c_temp > 1.0e-31:
c_temp = power(-1, i)/factorial(2*i + 1)
coef[i] = c_temp
i += 1
else:
raise ValueError("Argument not in [0, pi/4, pi, 3pi/2]")
return coef
# CONSTANTS
PI = 3.1415926535897932
SQRT_TWO = 1.41421356237309504880
if __name__ == "__main__":
print(power(4,2))
print(factorial(4))
print(abs(-0))
coef = coef_calc(0)
for x in coef:
print(coef[x] + "\n")
valueerrorlist indices must be integers or slices, not floatxis a member ofcoef, not an index into it. You just wantprint(x, "\n")