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?

Closed due to the fact that user194374, aleksandr barakin , Bald , Nicolas Chabanovsky July 8 at 10:59 am off topic.

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Community spirit, aleksandr barakin, Bald, Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • It does not solve the problem for you, here help to solve the problem - FeroxTL
  • The algorithm is obvious, conditionally: the результат = 0; i ОТ 1 ДО N: результат += 1/i результат = 0; i ОТ 1 ДО N: результат += 1/i . Show how they decided and what did not work out. - PinkTux
  • n = input () s = float (0) a = float for i in range (1, n + 1): a = 1 / is = s + ai = i + 1 print s - Sergey Kononenko
  • google mathematical limit =))))))))))))))))))))) - etki

2 answers 2

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 
  • one
    You can also do this: sum (1.0 / i for i in range (1, n)) - Chikiro

In one line:

 print(sum(1.0/i for i in range(1, int(input())+1)))