0

Print sum of number from 1to x

What is the error. It's not giving output a=0 def add(x): for i in range(1,x+1): a+=i print(a) d=int(input()) add(d)

14th Feb 2021, 5:22 AM
Ramana Prasad j
Ramana Prasad j - avatar
1 Réponse
+ 4
That's because you're printing the value of `a` before calling the add() function. You need to print it after calling add(d). Also, refrain from using global variables. Instead rely on variables local to the function and returning the results instead of making changes to a global variable.
14th Feb 2021, 5:41 AM
XXX
XXX - avatar