I am new to both Python and the Stack Overflow community, but I’m trying to learn. Anyway, I was watching a online guide about calling a function to calculate square root using Newton method. this is the formula What I cannot get my head around is that you need to update the variable in the “for loop” to get the result. Can anyone help me figuring out the logic behind this?
Thank you so much, this is my first time posting so I don’t know enough about the posting etiquette around here. Apology if I sound rude.
def compute_sqrt(n, num_loops): result = n/2 for i in range(num_loops): result_1 = (result + n/result) / 2 result = result_1 return result_1 computesqrt(n=100,num_loops=10)
Since you are using result with every iteration in the for loop, you would want to update result to be result_1, or else you will keep using the initial result value in each iteration