Hi there,

As a student I am new to python and am struggling with the following program I have been tasked to create. My code so far is like so. The idea is that the function will take a guess x, incorporate an arbitrary function f(x) and then take feps, a tolerance and max_it, the maximum number of iterations. The general idea is that this iterative approach will use the Newton_Raphson method to determin an approximation to the given value x. Example, def f(x): return x**2-2 and then newton(f,1.0,0.2,15) yields approx squared root of two - 1.414.......

``````def newton(f,x,feps,max_it):
it=0
def fprime(x):
return (f(x+feps/2.0)-f(x-feps/2.0))/feps
while abs(f(x))>feps:
x=x-f(x)/fprime(x)
it+=1
if it>max_it:
return None
return x``````

This seems to work well but I cannot get it to return the python special type - None if the number of iterations are exceeded during the while loop.

Any suggestions would be fantastic as I have now spent an embarrasing number of hours on it.

Can you place your code into a the code format, this way we can see how your indentations and such are. I can follow the code but I am unsure if you have just made a format mistake.

Sometimes adding a temporary test print will show you where the problem is.

Thanks for the edited post. It is a learning curve.

I am confident that there are no formatting errors, so in quick response:

When I use the above code and set the number of iterations so that None should be returned, newton(f,1.0,0.2,1), I find that the prompt comes back blank, i.e >>>

None

Is this any clearer?

P.S - Will learn how to submit code correctly - cheers Vegaseat.

Thanks for the edited post. It is a learning curve.

I am confident that there are no formatting errors, so in quick response:

When I use the above code and set the number of iterations so that None should be returned, newton(f,1.0,0.2,1), I find that the prompt comes back blank, i.e >>>

None

Is this any clearer?

P.S - Will learn how to submit code correctly - cheers Vegaseat.

From my understanding in Python a blank and None are treated as a boolean False, so as far as I can tell the program is doing exactly what it is suppose to do.

A None value is not displayed by the python shell, as in this example

``````>>> value = None
>>> value
>>>``````

A more useful thing to do would be to raise an exception when the number of iterations becomes too big. For example

``````def newton(f,x,feps,max_it):
it=0
def fprime(x):
return (f(x+feps/2.0)-f(x-feps/2.0))/feps
while abs(f(x))>feps:
x=x-f(x)/fprime(x)
it+=1
if it>max_it:
raise RuntimeError("Too many iterations.")
return x``````

However this will work in the Python shell ...

``````>>> value = None
>>> print(value)
None
>>>``````

Looks like the old shell has tripped up a few folks lately.

Both the DrPython IDE and the PyScripter IDE can run code from a buffer. I rather use that then the Python shell.

A None value is not displayed by the python shell, as in this example

``````>>> value = None
>>> value
>>>``````

However this will work in the Python shell ...

``````>>> value = None
>>> print(value)
None
>>>``````

Looks like the old shell has tripped up a few folks lately.

`>>> value` and `>>> print(value)` are sort of different.

`>>> value` and `>>> print(value)` are sort of different.

That was the point! Missed in the OP.

Thanks so much guys. This particular exercise, along with the other various exercises have finally been submitted. I now have a whole load more python challenges to deal with!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.