2

I want a program that starts with the main menu that branches off to different functions. At the end of each function I want the user to be asked 'Do you want to go back to the main menu?' if the user says no I want the program to terminate by stopping the main loop (I don't want to use sys.exit() or anything similar). Example code:

#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
    loop = True
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            program1()
        elif answer == 2:
            program2()
        elif answer == 3:
            program3()
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        loop = False #Here is the issue

#The rest of the programs would be the same

main()
1
  • 1
    You could just return True if they want to go back and return False if they don't, and then in the main loop set the value of loop to whatever is returned by the function that was just called. Your main loop will return to the while loop statement, see that loop == False, and terminate. Commented Feb 4, 2019 at 22:21

5 Answers 5

3

The problem is that you are trying to change a variable that was defined outside the scope of the program1 function. loop was defined inside main therefore only main can access it. There are several ways to fix this, you could declare loop outside (make it a global) or just make your program1 return a boolean to the caller function, e.g.:

def main():
    loop = True
    while loop:
        loop = program1()

def program1():
    itdontwork = input('''Do you want to go back to the menu? Y/N''')
    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        return False
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you want to try to do is have the different programs return a value.

The problem is that the functions get executed but return nothing. Your main function returns nothing; therefore the loop variable can never be updated to break the loop.

#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
    loop = True
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            return program1() # Return the output of this function
        elif answer == 2:
            return program2() # Return the output of this function
        elif answer == 3:
            return program3() # Return the output of this function
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        return False # Return the output of this function

#The rest of the programs would be the same

main()

Comments

0

The simplest way is to make your variable loop global

loop = True
def main():
    global loop
    while loop:
        print('''MENU CHOICE''')
        print('''1: go here''')
        print('''2: go there''')
        print('''3: You get the point''')
        print('''0: Terminate program''')
        print()

        try:
            answer = int(input('''I want to go to program: '''))
        except:
            print('''Not a valid menu choice, please try again''')
            print()

        if answer != 1 and answer != 2 and answer != 3 and answer != 0:
            print('''Not a valid menu choice, please try again''')
            print()
        elif answer == 1:
            program1()
        elif answer == 2:
            program2()
        elif answer == 3:
            program3()
        else:
            loop = False

def program1():
    global loop
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        loop = False #Here is the issue

#The rest of the programs would be the same

main()

Global will allow you to have the same w/r variable everywhere. Without global all variables a local.

Comments

-1
...
        elif answer == 1:
            loop = program1()
        elif answer == 2:
            loop = program2()
        elif answer == 3:
            loop = program3()
        else:
            loop = False

def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
        return 1 # back to menu...
    else:
        print('''SHUTTING DOWN''')
        return 0

This will get the data you need from the called functions to the loop as you want.

Comments

-2

You can raise ValueError('Words and stuff') and then trap on it.

if answer != 1 and answer != 2 and answer != 3 and answer != 0:
    print('''Not a valid menu choice, please try again''')
    print()
elif answer == 1:
    try:
        program1()
    except ValueError:
        break
elif answer == 2:
    try:
        program2()
    except ValueError:
        break
elif answer == 3:
    try:
        program3()
    except ValueError:
        break
else:
    loop = False


def program1():
    print('''This is program 1''')
    itdontwork = input('''Do you want to go back to the menu? Y/N''')

    if itdontwork == 'Y' or itdontwork == 'y':
        print()
    else:
        print('''SHUTTING DOWN''')
        # loop = False #Here is the issue
        raise ValueError('BOOM SHAKA LAKA!')

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.