0

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")
7
  • which line are you getting the valueerror Commented Jan 11, 2018 at 19:06
  • @Jai will add that in. It's the very last line. Commented Jan 11, 2018 at 19:07
  • 1
    Strange, when I run this code I get a TypeError, not a ValueError. What's your error message? Mine is list indices must be integers or slices, not float Commented Jan 11, 2018 at 19:09
  • 4
    x is a member of coef, not an index into it. You just want print(x, "\n") Commented Jan 11, 2018 at 19:13
  • @Kevin I mis-spoke, the error you see is what I got. Will edit... again. Commented Jan 11, 2018 at 19:14

2 Answers 2

4

When you do for x in coef:, x is the item iterating through coef and not its index, so when you do coef[x] it's expecting x to be an index of coef, which causes the error. If you want to get both the index and item, use for i, x in enumerate(coef): where i is the index and x is the item.

Your code is simple to fix though. Just change the last line to this instead:

print(x)

You don't need the "\n" since each print statement be a new line.

Sign up to request clarification or add additional context in comments.

Comments

0
  • I ran your code the variable coef has float values in it
  • What I would do is the follwing:

    for i in range(len(coef)):
        print(coef[i],"\n")
    
  • This way access the list by index and not by value

  • If you want to access it by value :

    for i in range(len(coef)):
        print(i,"\n")
    

6 Comments

That would just give you SyntaxError: keyword can't be an expression
dude I did a typo over there... try it now..,. I just edited my answer.... I ran it no error now
Ok, that works better. But isn't it kind of roundabout to iterate through the indices of a list just to access the elements of the list by index? Why not just do for x in in coef: print(x, "\n")?
yes you can do that for x in coef: print(x, "\n")... but i thought you wanted to access it by index
"i thought you wanted to access it by index". If by "you", you mean me specifically: nope, I prefer to iterate directly over values when possible. If by "you", you mean the OP: not as I interpret the question, but who knows how he really feels. If by "you", you mean the general community interested in idiomatic style: they, too, prefer to iterate directly over values when possible
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.