1

Good evening. Im having problem with my function. I have a menu with 4 or 5 different choices and i want to call my Usermeny() from main and make an if, elif statement so that if Choice == 1: do something.

But when i choose 1 i get this error "NameError: global name 'Choice' is not defined"

If i add global Choice in my Usermeny function i can call it from main but i thought my return would work?

in my main function it looks like this

def main():

    TRIES=25
    Usermeny()

    if Choice==1:
        DOSOMETHNIGHERE

And user menu looks like this

def Usermeny():

    while True:
        try:
            global Choice
            Choice=(int(input("What would you like to do: ")))

        except ValueError:
            print("You have to choose a number in the meny")
            continue

        if not Choice in range(1,5):
            print("You have to choose a number in the meny")
            continue

        return Choice
2
  • I can't reproduce your error with the code you provided... While the solution is almost certainly already posted below, perhaps if you can post an example of code which actually demonstrates the problem, we might be able to help you better.
    – mgilson
    Dec 26, 2013 at 18:06
  • also, choice < 1 or choice >= 5 is bound to be more efficient than not choice in range(1,5)
    – mgilson
    Dec 26, 2013 at 18:13

1 Answer 1

4

Remove the global declaration, it's not needed.

Assign the result of the Usermeny function to Choice in the main function:

def main():
    TRIES=25
    Choice = Usermeny()

    if Choice == 1:
        DOSOMETHNIGHERE
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.