我有一个关于麻省理工学院开放式课程Python Lecture 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这应该会阻止循环进一步工作。我的理解显然是错误的,但我看不到在哪里!
请停下..。
发布于 2018-12-19 20:42:38
您可以尝试使用Python可视化工具来查看每次迭代时guess的值如何变化。Try here.
https://stackoverflow.com/questions/53850250
复制相似问题