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()
return Trueif they want to go back andreturn Falseif they don't, and then in the main loop set the value ofloopto whatever is returned by the function that was just called. Your main loop will return to thewhile loopstatement, see thatloop == False, and terminate.