Given an integer N (> 0). Find the amount of 1 + 1/2 + 1/3 + ... + 1 / N
n = input() s = float(0) a = float for i in range(1, n + 1): a = 1 / i s = s + a i = i + 1 print s
what is wrong?
Given an integer N (> 0). Find the amount of 1 + 1/2 + 1/3 + ... + 1 / N
n = input() s = float(0) a = float for i in range(1, n + 1): a = 1 / i s = s + a i = i + 1 print s
what is wrong?
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
Wrong 3 things:
1) a = float
- why is it at all?
2) a = 1 / i
- in the 2nd Python it is an integer division, it is necessary a = 1.0 / i
3) i = i + 1
in the loop - too much You can simply:
s = 0.0 for i in range(1, n + 1): s += 1.0 / i print s
In one line:
print(sum(1.0/i for i in range(1, int(input())+1)))
Source: https://ru.stackoverflow.com/questions/542511/
All Articles
результат = 0; i ОТ 1 ДО N: результат += 1/i
результат = 0; i ОТ 1 ДО N: результат += 1/i
. Show how they decided and what did not work out. - PinkTux