1

I am new to python and have a very simple question:

x="hello World"
print_x()

def print_x():
    print(x)

this outputs NameError: name 'print_x' is not defined and I don't know why because i defined it right there.

(This is my first time writing a function)

3
  • 2
    It needs to be defined before you try and access it. Commented Apr 21, 2022 at 19:50
  • see also stackoverflow.com/questions/3754240/… Commented Apr 21, 2022 at 19:56
  • @Dennis ok thank you! Commented Apr 21, 2022 at 20:07

1 Answer 1

2

This is because you wrote your function AFTER you called it. Python is a scripting language, so you need to have the function declared before you call it.

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

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.