0
Good, I was here solving an exercise where the program asks the user for a positive number and calculates its square root using: +=(+ã/) / is an estimate for the square root of the number; + is the next estimate based on the previous estimate. The first estimate of the square root should be = U/ . The calculation of the next estimate should continue while the difference |+|>.
The code I have is:
# coding: iso-8859-1 -*-
import math
print('Programa que calcula a raiz quadrada')
print()
while True:
numero=eval(input('Introduza um número positivo: '))
if 0<numero:
break
else:
print('O número introduzido tem de ser positivo')
print()
Cont=0
Ri1=1
Ri=0
while abs(Ri1-Ri)>0.0001:
if Cont==0:
Ri=numero/2
Cont=Cont+1
else:
Ri1=(Ri+numero/Ri)/2
Ri=Ri1
Cont=Cont+1
print(numero)
print(Cont)
print(Ri1)
As I am still at the beginning of the programming I can only use if and while. Supposedly Estimate 1 = Ri=number/2 Estimate 2 = =(+ã/)/ Estimate 3 = +=(+ã/)/ ... one of the results that should give would be: Number=2; Number of Estimates =5; Result=1.414214
What mistake are you finding?
– Paulo Marques
I am not making a mistake, but the result is going wrong
– Diogo