Counter and for and while loops (Python) need to write a counter
I can't figure it out, I need to write a program to attach an image I can't, I'll write it like this:
Enter n calculate:
s=1/3^2+1/5^2+1/7^2+...+1/(2n+1)^2
Using the for
and while
{[5 loops]}
What am I doing wrong?
n=int(input("Введите n: "))
i=3
y=1
z = input("Выберите способ 1 - цикл while, 2 - цикл for: ")
if z == "1":
while i<2*n+1:
s=y/(round(i, 2*n+1))**2
print("S=", s)
i+=2
s=y/(round(i, 2*n+1))**2
print("S=", s)
break
else:
print("")
P.S. I didn't write the second part with the "for" operator
2
1 answers
n = 5
s = 0.0
for x in range(1, n + 1):
s += 1 / (2 * x + 1) ** 2
print('{0:.3f}'.format(s))
x = 1
s = 0.0
while x <= n:
s += 1 / (2 * x + 1) ** 2
x += 1
print('{0:.3f}'.format(s))
# 0.192
# 0.192
1
Author: slippyk, 2016-11-17 13:44:02