Search code examples
pythonpycharmnameerror

"NameError name 'x' is not defined" Pycharm using Python 3.9


new to python, I downloaded Pycharm and am using python verion 3.9 (most current version). I am having a problem where my terminal displays an error:

NameError: name 'run' is not defined

At one point I was able to get the console to ignore that error somehow but it gave a similar error for calling the test function.

I have done some research on function declaration and believe that this is the correct syntax. Attempted to find a similar question on this forum as well but could not seem to find my answer, so I apologize in advance if this is a duplicate or a common error.

if __name__ == '__main__':
    run()

def run():
    test()
    return

def test():
    print("test")
    return

Solution

  • define functions before the main

    def run():
        test()
        return
    
    def test():
        print("test")
        return
    if __name__ == '__main__':
        run()