-2

I am trying to write a simple program that will calculate the derivative of a polynomial function. Here is what I have. It may not be pretty, but for some reason the for loop is not iterable (whatever that means).

def main():

    n = int(input("please enter the degree of the polynomial: "))
    x = int(input("please enter the input for p(x): "))

    p = 2*x**n + 3*x**(n-1)

    v = int(input("please enter the order of the derivative: "))

    for i in v:
        p = 2*n*x**(n - i) + 3*(n-1)*x**((n - 1) - i)

    print p

main()
stderr
  • 8,567
  • 1
  • 34
  • 50
  • Instead of vaguely describing the problem, post the actual exception, with traceback. Or, if you're not getting an exception, tell us exactly what happens, and how it's different from what you expected. – abarnert Aug 13 '14 at 04:11

2 Answers2

3

You can only loop over iterables—things like lists that have multiple values.

If you're trying to loop over the first v integers, you need an iterable of the first v integers, not v itself:

for i in range(v):

In general, when you can't understand what a term means, look it up in the glossary. For iterable, it says:

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ...). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.

That's probably more detail than you wanted, but the first sentence explains it pretty well. A list, or an xrange object, or a file is something that can return members one at a time. A number is not; it doesn't even have members to return.*


* Well, it depends how you define numbers. If you, e.g., define 5 as {4, {4}}, then obviously 5 does have members to return… but they wouldn't exactly do you much good. Also, computing on Peano integers is very slow.

abarnert
  • 354,177
  • 51
  • 601
  • 671
-1

I ran your code and got:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "polynomial.py", line 17, in <module> 
 main()
File "polynomial.py", line 12, in main
for i in v:
TypeError: 'int' object is not iterable

You might do something like:

i = 1
while i < v + 1:
    p = ....
    i += 1
whitebeard
  • 1,071
  • 14
  • 19
  • If I remember correctly, the first step in taking a derivative is to take any exponent and bring it down, multiplying it times the coefficient. Then reduce the exponent by 1. The first iteration of the range function, as suggested above, would make i equal to 0, as documented here: https://docs.python.org/2/library/functions.html#range – whitebeard Aug 13 '14 at 05:48