#WSQ13

Babylonian Method

The function should receive a number and return floating point number. Obviously you should test your function, so create a main program that asks the user a value, calculates the square root and displays that.

def sqrt(n):
    x=n/2
    y=x+1
    while(x!=y):
        z=n/x
        y=x
        x=(x+z)/2
    return x

ans=”y”
while (ans==”y”):
    n = float(input(“Give me a number: “))
    if (n==0):
        print(“The square root of 0 is 0”)
    elif (n<0):
        print(“Error no square root for negative numbers.”)
    else:
        sqrta=sqrt(n)
        print (“The square root of”,n,”is”,sqrta)
    ans = str(input(“Do you want to try again? y/n: “))
if (ans==”n”):
    print (“Have a nice day.”)
else:
    print (“Options were y or n but whatever have a nice day.”)

 

CC BY 4.0 #WSQ13 by Luis Vargas is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.