-1

So I am running into an issue with my code for school. I don't know how to fix it.

Here is my code.

"""
Convert Newton’s method for approximating square roots in Project 1 to
a recursive function
named newton. (Hint: The estimate of the square
root should be passed as a second
argument to the function.)"""


import math
def newton(x, estimate):
    if abs (x-estimate ** 2) <= 0.000001:
            return estimate
    else:
        estimate = newton(x, (estimate + x/estimate)) /2
    return estimate                      
def main():
    while True:
        x = float(input('Enter a positive number or enter/return key to quit: '))
        if x == "":    
                break
        print("Newtons estimate of the sqaure root of ", x, "is: ", newton(x,estimate))
        print("The True value of the square root is: ", math.sqrt(x))
main()
Aaron
  • 7,351
  • 1
  • 24
  • 36
Heather
  • 11
  • 1
  • 1
  • 4
    Please format your code by highlighting it and press ctrl+k. Also, what specifically do you need help with? I don't see a question here. – Carcigenicate Oct 27 '17 at 16:30
  • 1
    please format this properly, it's tough to read. – acushner Oct 27 '17 at 16:30
  • "running into an issue" is everything you're willing to tell us? How are we supposed to help if you keep the issue secret? – Stefan Pochmann Oct 27 '17 at 16:38
  • You aren't giving the function an initial value for estimate. – TacoWilson Oct 27 '17 at 16:40
  • 2
    Hello Heather, and Welcome to StackOverflow. I wouldn't discouraged by any down-votes, they are rather common for first questions. You should take a look at [help] and [ask]. Ideally, your question should include a [mcve], which yours mostly does! The biggest improvement I would suggest is being explicit about the problems you are encountering. What output did you expect, and how is it different than the output you are currently getting? Are you getting an error? If so, you should post the full error message including the stack trace. "It doesn't work" tends to be recieved poorly around here. – juanpa.arrivillaga Oct 27 '17 at 16:45

1 Answers1

1

PROBLEM

The error is in your main program. You call

newton(x,estimate)

but estimate is undefined. The function expects it to have a value when you call it: the main program is responsible to assign one. Try this:

newton(x, x/2)

NEXT PROBLEM

This throws your program into an infinite loop. You failed to do the math correctly when you recur:

estimate = newton(x, (estimate + x/estimate)) /2

Your new estimate is supposed to be the average of the old one and the quotient, not their sum. You have to divide the sum by two. You divided the returned result by two. Try this:

estimate = newton(x, (estimate + x/estimate)/2)

Now you can do a straightforward example. Your program has a couple of other problems, but I'll leave those as an exercise for the student. Have fun.

Prune
  • 72,213
  • 14
  • 48
  • 72
  • Not worth an answer of my own, but I noticed the "hit enter to return" schema would fail on `float("")` with a value error, and that further makes the `if x == "":` line non-functional. – Aaron Oct 27 '17 at 17:16
  • Right ... "left as an exercise". You can't convert the empty string to `float`. – Prune Oct 27 '17 at 17:38