''' s = 0; k = 1; M = 100 while k < M: WANT K TO AT SOME POINT BE EQUAL TO 100, SO NEED TO HAVE A '<=' SIGN s += 1/k NEED FLOAT DIVISION, NOT INTS RUNS FOREVER B/C 'K' ISN'T INCREMENTED WITHIN LOOP print s ''' s = 0; k = 1; M = 100 while k <= M: s += 1.0/k k += 1 print s ''' Sample run: 5.18737751764 ''' s2 = 0; for k2 in range(1,101): s2 += 1.0/k2 print s2 ''' Sample run: 5.18737751764 '''
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question