1

I wrote a function and I try to call it but it gives me error

  NameError: name 'fun' is not defined

Example:

(p1,p2) = fun(price1,price2)

def fun(price1,price2):
  do something

4 Answers 4

3

You have to define functions before you can call them. Function definitions in Python are executable statements like any other, and are executed in the order they occur in the source file. Move your def fun block up ahead of the place where you call it.

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

Comments

3

In a standalone script like this, you'll have to define the function before you call it. So just change it to:

def fun(price1,price2):
    do something

(p1,p2) = fun(price1,price2)

Comments

0

Looks to me like the interpreter is running the the line of code calling the function before it is defining the function. You need to switch them around, or else wrap the function call code itself in a function like main() and call that after everything is defined.

Comments

0

If this is a standalone script, then you need to call fun after you have defined it.

def fun(price1,price2):
  do something

(p1,p2) = fun(price1,price2)

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.