NaturallyGreezy v2
NaturallyGreezy v2

Reputation: 65

How do I get this main menu NOT to loop #PYTHON

I have a main menu program on python3, with the code below. I want the code to work so that if either 1,2,3,4 it just runs the potential function. However, at the moment this only works for option 4 as it obviously doesn't loop. If I choose any other option, it runs the selected function but then also opts me to select from the main menu again.

def mainmenu ():
    choice = 0
    while choice != 4:

        print()
        print()
        print("Choose from this menu")
        print()
        print("1 - Maze Game")
        print("2 - Guessing Game")
        print("3 - Quiz")
        print("4 - Exit")
        print ()
        choice = input("Enter 1,2,3 or 4")

        if choice == "1":
             mazeGame()
        elif choice == "2":
             numberGuesser()
        elif choice == "3":
             quiz()
        elif choice == "4":
                print ("Thanks for using the program.")
        else:
            print("Please enter 1,2,3 or 4 only")


def mazeGame():
    print("Now running Maze game...")
def numberGuesser():
    print("Now running Guessing game")
def quiz():
    print("Now running quiz")

Upvotes: 0

Views: 270

Answers (4)

Rory Daulton
Rory Daulton

Reputation: 22564

Place a break command just after executing the desired function in choices 1, 2 and 3. In other words,

        if choice == "1":
            mazeGame()
            break
        elif choice == "2":
            numberGuesser()
            break
        elif choice == "3":
            quiz()
            break

That break will stop the loop, but just for those choices.

By the way, you have strange indentation for your code. Each additional level should be just 4 spaces, while your indentation is inconsistent. My code is indented 8 spaces for the outer lines, given that it is two levels in from the main level, and the next level is 4 more spaces--you have 5 spaces here.

Also, as @JkShaw points out, you should make sure all your "choices" are strings, never numbers, so change your while choice != 4: to while choice != "4":.

Upvotes: 1

Nikhil
Nikhil

Reputation: 23

def mainmenu ():
    choice = 0
    while choice != 4:

        print()
        print()
        print("Choose from this menu")
        print()
        print("1 - Maze Game")
        print("2 - Guessing Game")
        print("3 - Quiz")
        print("4 - Exit")
        print ()
        choice = input("Enter 1,2,3 or 4")

        if choice == 1:
             mazeGame()
             break
        if choice == 2:
             numberGuesser()
             break
        if choice == 3:
             quiz()
             break
        if choice == 4:
                print ("Thanks for using the program.")
        else:
            print("Please enter 1,2,3 or 4 only")


def mazeGame():
    print("Now running Maze game...")
def numberGuesser():
    print("Now running Guessing game")
def quiz():
    print("Now running quiz")
mainmenu()

Just add break and change "1" to 1(all of them) as input return an integer!

Upvotes: 0

JkShaw
JkShaw

Reputation: 1947

input() return type is str, for your case while choice != 4 will always be true, since you are comparing str with int

change

while choice != 4: to while choice != '4':

Upvotes: 0

Daniel
Daniel

Reputation: 42768

Write a function, that chooses the number, and a main, that does to processing:

def choose(options):
    while True:
        print()
        print()
        print("Choose from this menu")
        print()
        for num, text in enumerate(options, 1):
            print("{} - {}".format(num, text))
        print ()
        choice = input("Enter number")
        try:
            choice = int(choice)
            if 1 <= choice <= len(options):
                return choice
        except ValueError:
            print("Please enter a number")

def main():
    choice = choose(["Maze Game", "Guessing Game", "Quiz", "Exit"])
    if choice == 1:
            mazeGame()
    elif choice == 2:
            numberGuesser()
    elif choice == 3:
            quiz()
    elif choice == 4:
            print ("Thanks for using the program.")

Upvotes: 0

Related Questions