Python-无限While循环 [英] Python - Infinite While Loop
本文介绍了Python-无限While循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么底部的while循环是无限循环.
I do not understand why the while loop at the bottom is an infinite loop.
# User enters a positive integer number
user_input = int(input('Please enter a positive integer number: '))
# Make the epsilon value a constant
EPSILON_VALUE = 0.0001
# Choose an initial estimate - set the initial estimate e=x
estimate = user_input
# Evaluate the estimate - dividing the value x by your estimate e,
result = user_input / estimate
# Calculate the difference
difference = abs(estimate) - abs(result)
# If the difference is smaller than your epsilon value, then you've found the answer!
if difference <= EPSILON_VALUE:
print('Square Root')
print(result)
# Else the algorithm must iterate until it reaches the result
else:
while difference > EPSILON_VALUE:
result = ((user_input / estimate) + estimate) / 2
difference = abs(estimate) - abs(result)
print(difference)
推荐答案
这是因为您没有更改条件值.您的while
循环正在比较difference
和EPSILON_VALUE
,但是这些值在您的循环内都没有改变,因此该条件将始终评估为相同的值(true
).
It's because you're not changing the condition values. Your while
loop is comparing difference
and EPSILON_VALUE
, but neither of those values is changing inside your loop, so the condition will always evaluate the same (true
).
这篇关于Python-无限While循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文