python - 麻省理工学院公开课,第 3 讲 - 数学问题

标签 python python-3.x xcode

我对 MIT OCW Python 第 3 课有疑问。

根据简单的数学计算,她使用的代码应该不会成功。

## EXAMPLE: approximate cube root 
####################
#cube = 27
##cube = 8120601
##cube = 10000
#epsilon = 0.1
#guess = 0.0
#increment = 0.01
#num_guesses = 0
## look for close enough answer and make sure
## didn't accidentally skip the close enough bound
#while abs(guess**3 - cube) >= epsilon and guess <= cube:
#    guess += increment
#    num_guesses += 1
#print('num_guesses =', num_guesses)
#if abs(guess**3 - cube) >= epsilon:
#    print('Failed on cube root of', cube, "with these parameters.")
#else:
#    print(guess, 'is close to the cube root of', cube)

这是她使用的代码,我遇到的问题是理解这部分:

while abs(guess**3 - cube) >= epsilon and guess <= cube:
#    guess += increment

如果猜测是 0.0,立方是 27,增量是 0.01,那么这一项的数学应该是:

abs(0**3 - 27) = 27 #----- This is fine according to the code but the next step would be:#
abs(27.01**3 - 27) = 19677.878101

这应该会阻止循环进一步工作。我的理解显然在某处是错误的,但我看不到哪里!

请暂停...

最佳答案

您可以尝试使用 Python 可视化工具来查看 guess 的值如何随着每次迭代而变化。 Try here.

关于python - 麻省理工学院公开课,第 3 讲 - 数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53850250/

相关文章:

ios - 从地址簿中获取两个文本字段的电话号码

python - 如何用python显示SQLite3中的表结构?

python - 基于生成器的 for 循环和基于列表的 for 循环会产生不同的输出?

python - 根据每个列表的子集从列表列表中删除重复项

Xcode 4.3.2不会在调试器中显示完整的调用程序堆栈,而仅显示当前功能?

ios - UIKeyboard 未在 Xcode 模拟器中显示

python - 为什么 pytest 在使用 python -m test 运行时取消选择所有测试?

python - 无法使用scrapy从网页中获取不同列表的标题

python - 使用Python删除文件中的文件行数

python - 如果文本改变了 the 的大小并且 widget 改变了大小