python中while循环只能用来实现无限循环_Python-无限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)

解决方案

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值