+ 2
Please someone help me to understand this code???
I AM FACING THIS CODE AGAIN AND AGAIN IN EVERY CHALLENGE!! https://code.sololearn.com/cb32eaYyBrLg/?ref=app
6 Respuestas
+ 3
hi,
code is wrong bro
u have add the elements in d[] list but u have written the value y
def f(x):
d=[]
for i in range(x):
y= i+2
d.append(y)
return y
z= 3
w=f(z)
print(sum(w))
u have return the value y to w=f(z) which will return 4 as per ur code.
if u try to sum(4) then it will throw int object cannot be iterable.
so what u can do is return d[] to w= f(z)
then perform sum operation
def f(x):
d=[]
for i in range(x):
y= i+2
d.append(y)
return d
z= 3
w=f(z)
print(sum(w))
Which will print sum 9.
+ 2
okk thanks @nanda balkrishnan
+ 2
welcome bro!
+ 2
can u upvote my question??
i upvoted urs too!!
+ 1
I'm pretty sure that function should return d - otherwise you just get an error that "int is not iterable".
Now for the explanation:
-the function iterates over the values from 0 to x-1
-for each value i, we compute i+2 and append that to a list
-we return the resulting list from the function
-in the end, we calculate the sum of all elements in the list
In our example, we are calling f(3), which returns the following list:
[2,3,4]
And the sum of those numbers is 9.
+ 1
thanks @bogdan sass