JavaScript
x
7
1
menu = {
2
"1": "start_game()"
3
"2": "player_stats()"
4
"3": "high_scores()"
5
"0": "exit_game()"
6
}
7
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.
JavaScript
1
7
1
menu = {
2
"1": start_game,
3
"2": player_stats,
4
"3": high_scores,
5
"0": exit_game
6
}
7
Then you can do:
JavaScript
1
3
1
choice = input("Please enter 1, 2, 3, or 0: ")
2
menu[choice]()
3
to execute the user’s choice.