0

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")
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • which version of python you are using? – Hackaholic Apr 29 '17 at 10:01
  • Please be more clear. Do you want it to loop, asking again for a choice, if the user chooses something other than 1,2,3, or 4? If the user chooses 1,2, or 3 it does the function without looping, and if the user chooses 4 it just exits without doing anything other than saying thanks? – Rory Daulton Apr 29 '17 at 10:02
  • 2
    if you don't want to loop, don't write a loop. – Daniel Apr 29 '17 at 10:02
  • Apologies. I'd like it to loop if there is an incorrect number (neither 1,2,3,4 are chosen) so the user is prompted to type in the number again. Correct. I am using Python 3.6 – NaturallyGreezy v2 Apr 29 '17 at 10:05
  • `input()` return type is `str`, for your case `while choice != 4` will always be true, since you are comparing `string` with `int` – JkShaw Apr 29 '17 at 10:11
  • 1
    You may find this of interest: http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – PM 2Ring Apr 29 '17 at 10:21

4 Answers4

1

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":.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
0

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.")
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

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':

JkShaw
  • 1,927
  • 2
  • 13
  • 14
0
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!

Nikhil
  • 23
  • 6