What tells me to the command is? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What tells me to the command is?

Def fun(x): res=0 for i in range(x): res+=1 return res print(func(4)) Solution: 6 I read in the comments that the code is asking me to take each number in the range of 4 (so 0-4 with final four not counted). Why did they write res=0? What purpose does that do? Thanks for any help:).

28th Mar 2019, 2:42 AM
tristach605
tristach605 - avatar
2 Answers
+ 3
Whenever a variable is declared blank, it has NULL value if you try to print it. So when we have operations like addition, multiplication and stuff, we need to set an initial value, like in the above case we start with zero, so res = 0 And range works like this : range(n) : 0,1,2,3......,(n-1)
28th Mar 2019, 3:44 AM
RZK 022
RZK 022 - avatar
+ 1
Thanks JM and Gamma👍🏻
28th Mar 2019, 6:13 AM
tristach605
tristach605 - avatar