+ 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:).
2 odpowiedzi
+ 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)
+ 1
Thanks JM and Gamma👍🏻