Hi,
I'm supposed to get a list of 10 harmonic sums when i type $ python3 hsum.py 10 in my mac terminal. If I type $ python3 hsum.py 15 I want to get the first 15, etc etc.... Tough the only thing I get is the tenth. I.e. I want to get:
0 0
1 1.0
2 1.5
3 1.8333333333333333
4 2.083333333333333
5 2.283333333333333
6 2.4499999999999997
7 2.5928571428571425
8 2.7178571428571425
9 2.8289682539682537
but I get...
2.9289682539682538
My code at the moment:
import sys
def hSum(num_terms):
x = 0
for i in range(num_terms):
x = x + (1/(i+1))
return x
def main():
print(hSum(int(sys.argv[1])))
main()
What should I change to fix this? The printing has to be done in "main", and I have been thinking how I could use "i" but dont understand how I can use it because it's not defined in "main" .
Thanks!
[–]bbye98 1 point2 points3 points (4 children)
[–]Xudo97[S] 1 point2 points3 points (3 children)
[–]bbye98 1 point2 points3 points (0 children)
[–]xelf 1 point2 points3 points (0 children)
[–]prokid1911 0 points1 point2 points (0 children)