+ 1

How could I calculate the sum of 1 to n??

def f(N): N=0 try: N=int(N) except ValueError: print('You did not enter a valid integer') sys.exit(0) if N<=0: return 0 else: y=0 x=1 while y<N: y=y+x x=x+1 print(N) I don't know what's wrong with it....sad

22nd Jan 2018, 7:02 PM
Xinjing
2 odpowiedzi
+ 16
just print n(n+1)/2 //if n>=1 && n%1.0==0.0 else print "enter a natural number" //don't know python 😕 , but u apply this to get better solution
22nd Jan 2018, 7:10 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
- 1
I need to define f(n), and if n<0 return to 0 and raise an error if n is not a integar
22nd Jan 2018, 7:13 PM
Xinjing