Home > Mobile >  Python NameError: name 'print_x' is not defined
Python NameError: name 'print_x' is not defined

Time:04-22

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)

CodePudding user response:

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.

  • Related