开发者

Understanding Python code to try and compute a square root [closed]

开发者 https://www.devze.com 2022-12-07 21:56 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 39 mins ago.

Improve this question

I'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.

0

精彩评论

暂无评论...
验证码 换一张
取 消