- 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
17 Réponses
+ 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
+ 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
+ 7
That's right, Abishek. However knowing how to get 6 is for more important than just the correct answer
+ 4
answer is 3 not 6
+ 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
+ 2
6 is the correct answer
+ 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)
+ 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
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))
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))
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
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
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
- 1
can you explain the meaning and how exactly you got it?
- 1
How the answer is 6?
- 2
answer is 6
- 5
thank you guys ...
I plotted 6 and it was correct