Search code examples
pythonfunctionloopsmenuprogram-entry-point

Python main function loop with a menu function is not working?


I am currently a college student taking a python class. Our assignment is to create this program with functions. The main function calls the menu and then we write a loop in the main function to access the other functions based on the user response in the menu function.

I can't seem to get my loop to work. When I select a menu option nothing happens. For now, I just have print statements to test the calling of the functions. I want to make sure this works before I write the functions.

If anyone has an example of what the loop should look like to call the functions it would help me a lot.

def GetChoice():
    #Function to present the user menu and get their choice

    #local variables
    UserChoice = str()

    #Display menu and get choice
    print()
    print("Select one of the options listed below:  ")
    print("\tP\t==\tPrint Data")
    print("\tA\t==\tGet Averages")
    print("\tAZ\t==\tAverage Per Zone")
    print("\tAL\t==\tAbove Levels by Zone")
    print("\tBL\t==\tBelow Levels")
    print("\tQ\t==\tQuit")
    print()
    UserChoice = input("Enter choice:  ")
    print()
    UserChoice = UserChoice.upper()

    return UserChoice

def PrintData():
    print("test, test, test")

def AverageLevels():
    print("test, test, test")

def AveragePerZone():
    print("test, test, test")

def AboveLevels():
    print("test, test, test")

def BelowLevels():
    print("test, test, test")

def main():
    Choice = str()

    #call GetChoice function

    GetChoice()

    #Loop until user quits

    if Choice == 'P':
        PrintData()
    elif Choice == 'A':
        AverageLevels()
    elif Choice == 'AZ':
        AveragePerZone()
    elif Choice == 'AL':
        AboveLevels()
    elif Choice == 'BL':
        BelowLevels()


main()

Solution

  • The loop should start with the following:

    while True:
        Choice = GetChoice()
    

    And the if conditions for the menu should follow at the same indent.

    If you want to add an option to quit the program, add another elif statement as below:

    elif Choice == "Q":
        break
    

    This will exit the loop and thus end the program.

    (Excuse the many edits - using mobile)