what is the output of def func(x): res = 0 for i in range(x): res += i return res print(func(4))? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 11

what is the output of def func(x): res = 0 for i in range(x): res += i return res print(func(4))?

the code is from python

7th Jun 2018, 2:21 PM
Saurav nalla
17 Answers
+ 10
10 EDIT: in range function, Loop iterates till limit -1, in this case, it will iterate till 3. So the answer would be 3+2+1 = 6
7th Jun 2018, 2:22 PM
‎ ‏‏‎Anonymous Guy
+ 11
The loop runs four times 0-3 res += i res = res + i res = 0 + 0 = 0 res = 0 + 1 = 1 res = 1 + 2 = 3 res = 3 + 3 = 6
29th May 2020, 11:17 PM
Ali
+ 7
That's right, Abishek. However knowing how to get 6 is for more important than just the correct answer
4th Jul 2020, 5:21 AM
Ali
+ 4
answer is 3 not 6
22nd Dec 2022, 2:47 PM
Jeel Bhadani
Jeel Bhadani - avatar
+ 2
*pass 4 as parameter to function func *res is set to 0 *a for loop (runs 4 times), i for each number between 0 and 4 *res = res + i *returns the value of res
7th Jun 2018, 2:33 PM
‎ ‏‏‎Anonymous Guy
+ 2
6 is the correct answer
4th Jul 2020, 4:03 AM
Abhishek Upadhayay
Abhishek Upadhayay - avatar
+ 1
In range function, Loop iterates till limit -1, in this case, it will iterate till 3 therefore 1+2+3=6 (6 is the correct answer)
17th Jul 2020, 8:55 AM
Amani TC
Amani TC - avatar
+ 1
What's the output of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4)) The loop runs four times 0-3 res += i res = res + i res = 0 + 0 = 0 res = 0 + 1 = 1 res = 1 + 2 = 3 res = 3 + 3 = 6
12th Mar 2021, 6:45 PM
Syed Fahad Ahmed
Syed Fahad Ahmed - avatar
0
alright so I thought I understood the logic here but I am clearly missing something. def func(x): res = 0 for i in range(x): res += i return res print(func(4))
16th Jul 2020, 8:19 AM
Tyler Sandiford
Tyler Sandiford - avatar
0
alright so I thought I understood the logic here but I am clearly missing something. def func(x): res = 0 for i in range(x): res += i return res print(func(4))
16th Jul 2020, 8:19 AM
Tyler Sandiford
Tyler Sandiford - avatar
0
What is the output of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4) Anyone give answer
21st Oct 2020, 8:59 AM
ABY ABRAHAM
ABY ABRAHAM - avatar
0
What is the output of this code? def func(x): res = 0 for i in range(x): res += i return res print(func(4)) =6
10th Jul 2021, 1:49 AM
Bimsara Senavirathna
Bimsara Senavirathna - avatar
0
the output will be 3 as last element 3 is excluded. es += i res = res + i res = 0 + 0 = 0 res = 0 + 1 = 1 res = 1 + 2 = 3
28th Oct 2022, 5:47 AM
Shoaib Hayder
Shoaib Hayder - avatar
- 1
can you explain the meaning and how exactly you got it?
7th Jun 2018, 2:25 PM
Saurav nalla
- 1
How the answer is 6?
13th Dec 2019, 11:09 AM
Pradip Karmakar
Pradip Karmakar - avatar
- 2
answer is 6
12th Jun 2018, 4:03 PM
arthi krithika
arthi krithika - avatar
- 5
thank you guys ... I plotted 6 and it was correct
1st Sep 2018, 4:47 AM
Harshit Sharma
Harshit Sharma - avatar