-2

I have written a menu that runs before I am testing it will only run the first if and not any of the following elif lines.

An example being when asked to make my input in the first question in the menu, if I type exit (or any capitalised variation) it will output 'Goodbye' as expected and stop running but if I type in 'color', 'colour', 'play' or make an invalid entry nothing happens and the script stops running.

print("Python Adventure\n")


def menu():  # This is the menu text for options before running the game
    option_1 = input("Select an option:\nPlay  Color  Exit\nType:")
    if option_1.lower() == "exit":
        print("\nGoodbye")
        exit()
    elif option_1.lower() == "color" or "colour":  # color options here
        def color():
            color_1 = input("Play with color? yes/no\nType:")
            if color_1.lower() == "n" or "no":
                print("Color off\n")
                menu()
            elif color_1.lower() == "y" or "yes":
                print("Color on\n")
                menu()
            elif color_1.lower() != "":
                print("Not recognised please try again.")
                color()
        color()
    elif option_1.lower() == "play": # Text based game will start here.
        print("Running: Python Woods")  
    elif option_1.lower() != "":  # Invalid entry trigger.
        print("Invalid entry, try again.")
        menu()


menu()

Please feel welcome to correct me on any terminology and formatting too. Any learning is helpful.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Nocturn
  • 1
  • 2
  • 2
    The problem is that you call menu() from within menu(), so as you use the program it just gets deeper and deeper into the 'stack'. – labroid Apr 09 '21 at 18:45
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Maurice Meyer Apr 09 '21 at 18:48
  • Two posts that would be very helpful to you: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) and [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – G. Anderson Apr 09 '21 at 18:49
  • Also, rather than defining your `color()` function inside an if statement in another function, it would help your code organization to define it outside the `menu` function then just call it in the other function. Out of curiosity, why do you have menu calling color and color calling menu, which can then call color, which can call menu... – G. Anderson Apr 09 '21 at 18:51
  • Thanks everyone for the responses I'll be sure to explore the links and changing my method. Also in reply to G. Anderson, mainly it's because I don't know any better yet, my idea was to loop back to the colour input again if a wrong input was made but to be honest its my 3rd day learning python so I'm boud to be making a mess of it in some places. Thanks again. – Nocturn Apr 09 '21 at 20:19

1 Answers1

1

Here is a better design, which most of the commenters have implied:

print("Python Adventure\n")

def color():
    color_1 = input("Play with color? yes/no\nType:")
    if color_1.lower() in ("n", "no"):
        print("Color off\n")
    elif color_1.lower() in ("y", "yes"):
        print("Color on\n")
    else:
        print("Not recognised please try again.")
        return True
    return False

def menu():  # This is the menu text for options before running the game
    option_1 = input("Select an option:\nPlay  Color  Exit\nType:")
    if option_1.lower() == "exit":
        print("\nGoodbye")
        return False
    elif option_1.lower() in ("color", "colour"):  # color options here
        while color():
            pass
    elif option_1.lower() == "play": # Text based game will start here.
        print("Running: Python Woods")  
    elif option_1.lower() != "":  # Invalid entry trigger.
        print("Invalid entry, try again.")
    return True

while menu():
    pass
JonSG
  • 10,542
  • 2
  • 25
  • 36
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • The if statements aren't doing what you intend. You really want, for example, `if color_1.lower() in ['n', 'no']` or `if color_1.lower() == 'n' or color_1.lower() == 'no'` – labroid Apr 09 '21 at 19:05
  • Ack, I just solved the immediate problem. I guess I'll tweak it to make it work. – Tim Roberts Apr 09 '21 at 19:06
  • Thank you both for your time, I'll be sure to explore this further. – Nocturn Apr 09 '21 at 20:20