0
def and return problem
what is the highest number output? def print_nums(x): for i in range(x): print(i) return print_nums(10) ###the answer is 0 here is my solution thought: range 0.1.2.3.4.5.6.7.8.9, why is 0? what is the output? def func(x): res = 0 for i in range(x): res += i return res print(func(4)) ###the answer is 6 Range 0.1.2.3 res=4+0, 4+1, 4+2, 4+3 then why 6?
3 Réponses
+ 3
for i in range(x):
print(i)
Here if x=10 then I values in range 0 to x-1.
So 0 to 9.
range(min, max) if you not specify min, value then it starts from 0.
If you want start from 1, (or some specific min value, mention there as)
write range(1,10)
In next,
res=0
for i in range(4)
res+=i
Here, i is 0 to 3. #(4-1)
Each time I added to res..
0+0=0.
0+1=1
1+2=3
3+3=6
0
Jayakrishna thanks, I got it now
0
Arvonce You're welcome