[Tutor] Python Program: Newton's Method

Kent Johnson kent37 at tds.net
Mon Jan 26 04:13:08 CET 2009


On Sun, Jan 25, 2009 at 6:11 PM, Donna Ibarra <donnaibarra at gmail.com> wrote:
> Hello,
>
> I need to write a program that implements Newton's method ((guess + x/guess)
> / (2)). The program should prompt the user for the value to find the
> squareroot of (x) and the number of times to improve the guess. Starting
> with a guess value of x/2, your program should loop the specified number of
> times applying newton's method and report the final value of guess. You
> should also subtract your estimate from the value of math.sqrt() to show how
> close it is.
>
> So far.. I got:
>
> Help with Code Tags
> (Toggle Plain Text)
>
> import math
>
>
> def main():
>     value, guess = input("Please enter the value, and the number of times to
> loop: ")
>
>     for i in range(guess):
>         top = guess + value / guess
>         final_value = float(top) / 2
>
>
>     close = final_value - math.sqrt(x)
>     close_two = math.sqrt(x)
>
>     print "The guess is", final_value, "which is", close, "away from",
> close_two
>
> main()
>
> Could you please help me out? Thanks

This looks like homework, so we are limited in how we can help. Some hints:
What does the variable guess represent in the input line? How about in
the for loop?
What does final_value represent? why do you divide it by 2?

Kent


More information about the Tutor mailing list