Square root in python

light2

Recruit
In Python, I'm working on a math issue that approximates the square root of an integer using Newton's guess and check method. The user must enter a number, an initial guess for the number, and the number of times they wish to check their answer before returning.
Here is my code, with comments to assist (each function is listed in the order in which it is used):
Python:
# This program approximates the square root of a number (entered by the user)
# using Newton's method (guess-and-check). I started with one long function,
# but after research, have attempted to apply smaller functions on top of each
# other.
# * NEED TO: call functions properly; implement a counting loop so the
# goodGuess function can only be accessed the certain # of times the user
# specifies. Even if the - .001 range isn't reached, it should return.

# sqrtNewt is basically the main, which initiates user input.

def sqrtNewt():
    # c equals a running count initiated at the beginning of the program, to
    # use variable count.
    print("This will approximate the square root of a number, using a guess-and-check process.")
    x = eval(input("Please type in a positive number to find the square root of: "))
    guess = eval(input("Please type in a guess for the square root of the number you entered: "))
    count = eval(input("Please enter how many times would you like this program to improve your initial guess: "))
    avg = average(guess, x)
    g, avg = improveG(guess, x)
    final = goodGuess(avg, x)
    guess = square_root(guess, x, count)
    compare(guess, x)


# Average function is called; is the first step that gives an initial average,
# which implements through smaller layers of simple functions stacked on each
# other.
def average(guess, x) :
    return ((guess + x) / 3)

# An improvement function which builds upon the original average function.
def improveG(guess, x) :
    return average(guess, x/guess)

# A function which determines if the difference between guess X guess minus the
# original number results in an absolute vale less than 0.001. Not taking
# absolute values (like if guess times guess was greater than x) might result
# in errors
from math import *
def goodGuess(avg, x) :
    num = abs(avg * avg - x)
    return (num < 0.001)

# A function that, if not satisfied, continues to "tap" other functions for
# better guess outputs. i.e. as long as the guess is not good enough, keep
# improving the guess.
def square_root(guess, x, count) :
    while(not goodGuess(avg, x)):
        c = 0
        c = c + 2
        if (c < count):
            guess = improveG(guess, x)
        elif (c == count):
            return guess
        else :
            pass

# Function is used to check the difference between guess and the sqrt method
# applied to the user input.
import math
def compare(guess, x):
    diff = math.sqrt(x) - guess
    print("The following is the difference between the approximation")
    print("and the Math.sqrt method, not rounded:", diff)

sqrtNewt()

I divided it up into a lot of smaller functions to make things easier and get to know Python (I only started learning the language a couple of months ago); the problem now is that I'm having difficulties calling each function and passing the numbers through.
I'm currently getting the following error: g, avg = betterG (guess, x)
The 'float' object is not iterable, resulting in a TypeError. The final function subtracts the final estimate iteration from the math square root approach described in this handbook and returns the total difference. Is this even possible? If you can offer working code with suggestions, that would be great. Again, I'm a rookie, so please excuse any misunderstandings or blatant mistakes.
 
Last edited by a moderator:
def average(guess, x) : return ((guess + x) / 3)
The average function returns just one value which is a float

g, avg = improveG(guess, x)
But you are expecting it to return two values (since improveG is returning average()). And since float is not an iterable, meaning it doesn't have indices 0,1,2... (you can read more about it here) you get an error.

This is what your code probably should be doing (not sure)

Python:
def someFunction():
    return 1,2

# OR

def someFunction():
    return (1,2)


val1 , val2 = someFunction()

print(val1,val2) # 1 2
 
Back
Top