-1

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

1
  • 3
    Please read a Python introduction. We cannot provide the tutoring that would be required to clear up all the misconceptions in this post. But for starters, your function does not return anything, that's why you get None. Commented Jul 1, 2022 at 12:05

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.