Understanding Python code to try and compute a square root [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 39 mins ago.
Improve this questionI'm completely new to programming and have to learn it because of a math course I'm taking that uses Python. I'm stuck at understanding the pr开发者_开发技巧ocess of this code.(see below)
So this is how I understand the program so far (I think):
The program keeps running until the requirement of (20-x**2) > 0.0001
is met, and the x-value is changed for each 'run through'. But why not (20-x**2) = 0
? Wouldn't that be a more right answer? And what does 'y' do in this code?
Hope someone can enlighten me!
x = 3
y = 6
while abs(20-x**2) > 0.0001:
x = (x+y)/2
y = 20/x
print("The square root of 20 is: ", round(x,4))
This is a question where I only need to understand the theory.
精彩评论