Skip to content
Advertisement

How to make menu using dict evaluation for small python guessing game

menu = {
"1": "start_game()"
"2": "player_stats()"
"3": "high_scores()"
"0": "exit_game()"
}

So let’s say if user inputs 1 in menu screen, I want to call start_game() function, I know there is other way to do this but just interested in this particular way because I think its neat.

Advertisement

Answer

Don’t put strings in the dictionary, put references to the function.

menu = {
    "1": start_game,
    "2": player_stats,
    "3": high_scores,
    "0": exit_game
}

Then you can do:

choice = input("Please enter 1, 2, 3, or 0: ")
menu[choice]()

to execute the user’s choice.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement