Python 3 - Modules Quiz - Question #5 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python 3 - Modules Quiz - Question #5

The answer to this quiz question is "6." However, I cannot seem to figure out why that is so... We define the function to take the argument "x." Then we set "res" to mean zero. Next, for "i" in a range of this same variable "x," we will add "res," which is zero. This literally doesn't seem to add anything. (Zero plus any number results in the original number--the zero principle of addition, right?) Then it says to "return 'res'," which makes "res" the new number for "i?" Finally, the last line runs through the program using 4 as the argument. To "i" in a range of 4, add 0. That gives us 4, no? But the answer is 6....Hmm. _____ "What is the output of this code?" def func(x): res = 0 for i in range(x): res += i return res print(func(4)) ____

2nd Jun 2018, 6:04 PM
ubuntu+mate
ubuntu+mate - avatar
4 Answers
+ 7
numbers in range (4) are 0,1,2,3 res= 0 for loop : i = 0 , res= res+i = 0+0 = 0 i = 1, res= res+i = 0+1 = 1 i = 2, res= res+i = 1+2 = 3 i = 3, res= res+i = 3+3 = 6 hope it helps :D
2nd Jun 2018, 6:12 PM
voja
voja - avatar
+ 3
This does help. Thanks much Voja!
3rd Jun 2018, 9:36 PM
ubuntu+mate
ubuntu+mate - avatar
+ 2
8
22nd Jun 2021, 3:29 PM
Shivani Sharma
Shivani Sharma - avatar
0
the answer is 8
16th Aug 2022, 6:42 AM
Iman Jafari
Iman Jafari - avatar