samedi 3 février 2018

Python - Infinite While Loop

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)

Aucun commentaire:

Enregistrer un commentaire