def func(x): res = 0 for i in range(x): res += i return res print(func(7)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

12th Aug 2020, 7:46 AM
Shubham Kumar
Shubham Kumar - avatar
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!
12th Aug 2020, 12:01 PM
Lothar
Lothar - avatar
+ 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
12th Aug 2020, 7:55 AM
Anton Böhler
Anton Böhler - avatar