0
def func(x): res = 0 for i in range(x): res += i return res print(func(7))
can someone please explain how is this program working ? its output is 21
2 Answers
+ 3
Shubham Kumar , please keep in mind:
- use appropriate tag for programming language
- do not put code in title. Place code line by line in text area ( small snippets), for larger codes please use playground to store, and then post a link to it here.
Thanks!
+ 1
a function is defined that adds 1 + 2 + ... until the given input
afterwards the function is called with the input 7 and the result is printed
Since 1 + 2 + 3 + 4 + 5 + 6 is 21 the output of the program is 21