I want to assign value to function of below code:
def adds(y):
for i in y:
z = i 1
print(z)
x = [1, 2, 3]
adds(x)
output:
2
3
4
But when i tried to assigned the result to function with creating instance such:
# print(z) commented
p = adds(x)
print(p)
output:
None
expected output:
2
3
4
gives the return inside for loop gives output: 2
gives the return inside function block or set variable z to global inside the for loop block, and recall it in the outside gives same output: 4
How do to achieve the expected output: 2 3 4, from the return value to function of above code
CodePudding user response:
As the function is printing the result, so no need to store the function value add(z) in variable so can return the value in function by using (return z) in function, Then you can store in a variable and print it. Thanks
CodePudding user response:
from https://stackoverflow.com/a/39366192/18980456
def adds(y):
z = [i 1 for i in y]
return z
x = [1, 2, 3]
p = adds(x)
print(p)