1

for an assignment, I made a menu and must have it work in a way to execute multiple functions. However, the problem is that when I use the menu and put in an answer that doesn't exist, I cannot get it to work correctly. So when I re-enter an option number for "Incorrect option, try again: ", I do not get the number re-evaluated to execute. Since my code is far from finished, right now I want to be able to choose "4" as an input and get "Incorrect option, try again" as an output and input "1" into this to get the output "Choose the level of difficulty".

def main_menu():
print(10*"=","GAME",10*"=",'\n')
print("1. Choose level of difficulty ")
print("2. Start Game")
print("3. Exit the Game",'\n')
print("Current Difficulty: /5")
print("Highest Score Reached:",'\n')
option=input("Enter an option: ")
return option

def user_input():
while True:
    try:
        if option==1:
            difficulty()
            break 
        elif option==2:
            start()
            break
        elif option==3:
            exit()
            break
    except: 
        option=input("Incorrect option, try again: ")

def difficulty():
 d=int(input("Choose level of difficulty: "))
 if 1<=d<=5:
     start()
 else:
     int(input("Incorrect option, try again: "))
     #difficulty()
 return d
AM.AM
  • 9
  • 2
  • Please post your code as text, not image. It helps others give a try without manually typing it. See [link](https://stackoverflow.com/help/mcve) on how to produce a Minimal, Complete, and Verifiable example. – Ignatius Feb 19 '19 at 01:38
  • 1
    [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – martineau Feb 19 '19 at 01:39
  • 1
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – James Feb 19 '19 at 01:51
  • Instead of `try/except` which is for [handling exceptions](https://docs.python.org/3/tutorial/errors.html#handling-exceptions), put the *incorrect option* statement in an [else clause](https://docs.python.org/3/tutorial/controlflow.html#if-statements). – wwii Feb 19 '19 at 01:52

1 Answers1

0

Here a is modified version of your code which I believe does what you are looking for.

def main_menu():
    print(10 * "=", "GAME", 10 * "=", '\n')
    print("Current Difficulty: /5")
    print("Highest Score Reached:", '\n')

    while True:
        print("1. Choose level of difficulty")
        print("2. Start Game")
        print("3. Exit the Game", '\n')

        try:
            option = int(input("Enter an option: "))

            if option == 1:
                difficulty()

            elif option == 2:
                start()

            elif option == 3:
                return

            else:
                print("Incorrect option, try again!")

        except ValueError:
            print("Invalid option.")


def difficulty():
    try:
        d = int(input("Choose level of difficulty: "))

        if 1 <= d <= 5:
            print(d)

            start()
        else:
            print("Incorrect option, try again.")

    except ValueError:
        print("Invalid value.")


def start():
    print("Starting game...")


if __name__ == "__main__":
    main_menu()

Let me know if anything is misunderstood or mistaken.

Omari Celestine
  • 1,405
  • 1
  • 11
  • 21