-2

I am newbie to python i wrote a small function which is resulting in error can you please let me know what is the mistake i am doing

def cost(input):

    output=input*2
    next=output*3
    return      output,next

print output
print next

Namerror name 'output' is not defined

2
  • Possible duplicate of accessing a variable from outside the function in python Commented Sep 24, 2017 at 18:30
  • 2
    You haven't called the cost function yet. use output,next = cost(10) before printing output variable. Commented Sep 24, 2017 at 18:31

4 Answers 4

0

Output is not defined since it is local to the function and the function hasn't even run. To get it globally, above print output you would put:

output, next = cost(1.12)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to call the function first. Both output and next are defined within the function and can not be accessed directly from outside.

Comments

0
print output

There is no variable called output for python to display. The output variable you have is inside the function, which is not accessible outside.

Comments

0

the problem is that the scope of the output and next variable is within the function, they cannot be referenced outside the function. If you want to print the result, just call the cost function in the print statement:

print cost(100)

Comments

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.