tauhtauhsauce
tauhtauhsauce

Reputation: 117

Python 3 builtins.NameError: global name --- is not defined -

Need some guidance here please. This is probably a stupid mistake, but I'm getting the "builtins.NameError: global name -- is not defined" error and I'm not seeing why - I'm still learning the language :).

Here is my code:

def option(x):
    if x == "E":
        enter()
    elif x == "V":
        view()
    else:
        exit()

def enter():
    msg = input("Enter the message\n")
    main()

def view():
    ##if msg == 0:
    #print("no message yet")
    #main()
    #else:
    print("The message is:", msg )
    main()        

def exit():
    print("Goodbye!")


def main():
    print("Welcome to BBS")
    print("MENU")
    print("(E)nter a message")
    print("(V)iew message")
    print("e(X)it")
    print("Enter your selection:")
    choice = input()
    option(choice)
    #msg = 0

main()

My problem is that I'm getting this even though I'm choosing the "E" option first:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 36, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 33, in main
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 3, in option
    pass
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 11, in enter
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 33, in main
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 5, in option
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 18, in view
builtins.NameError: global name 'msg' is not defined

Could I please be guided? I've been searching for info and haven't found anything, and my conclusion is that it is probably something really stupid and noobish.

Also, as you guys can see from what I've commented out, I've attempted to restrict "view" from giving an error by checking that msg != 0 -- I made msg = 0 in main() - this obviously doesn't work because after going through enter() it goes back to main() and makes msg == 0 again. Could you guys link me to a page/guide that will help me understand how to solve this? I don't want to be spoon fed that much..

Thanks,

Itachi

Upvotes: 0

Views: 7015

Answers (2)

Sophie Swett
Sophie Swett

Reputation: 3388

The problem here is that msg inside enter() is a local variable: it's created when the enter() function runs, and it ceases to exist when enter() returns. Normally, whenever you set a variable inside a function, you're setting a local variable. If you want to set a global variable, which retains its value even after the function returns, use a global statement:

def enter():
    global msg
    msg = input("Enter the message\n")
    main()

That said, global variables are often not the best way to do things. It may be better to have the enter() function return the message instead of storing it in a variable.

Upvotes: 2

Brian Cain
Brian Cain

Reputation: 14619

msg is a name that's not scoped to be used anywhere. That's why you get the NameError.

Each one of the functions you've created should stand alone and have straightforward inputs and outputs.

main is your entry point and it should call other functions as appropriate.

Those functions will return to their caller when their execution is complete. They can and in some cases should return some amount of data back to their caller.

For example, here's a subset of your problem showing how main invokes view and then returns:

def view(text):
    if not text:
        print("no message yet")
    else:
        print("The message is:", msg )

def main():
    print("Welcome to BBS")
    print("MENU")
    print("(E)nter a message")
    print("(V)iew message")
    print("e(X)it")
    print("Enter your selection:")
    while not exiting:
        choice = input()
        view(choice)
        exiting = True # TODO: set this based on the value in choice

Upvotes: 0

Related Questions