Skip to content
← Back to Community
How do you make a menu in python
Profile icon
ProbablyJester

I'm quite new to coding and I've made this very simple game for a computer science class and I'm wondering how I can make the menu work. I just can't figure out how (in the menu) to make it so after you set an option (such as grid size) how to go back to the "menu" instead of just starting the game. I've heard of methods and stuff but I don't know how they work in python.

Voters
Profile icon
ProbablyJester
Comments
hotnewtop
Profile icon
Squirrel777

When it comes to menus, this is what I do:

print("===Menu===") print("1. Option A") print("2. Option B") print("3. Option C") choice = input("Enter the number of your choice:") if choice == 1: print("You chose option 1!") else if choice == 2: print("You chose option 2!") else if choice == 3: print("You chose option 3!")

I Hope this helps!

Profile icon
MarquizeWhite

@Squirrel777 but for that, you would need elif and not else if

Profile icon
RYANTADIPARTHI

Solutin

you can make menus with functions, or no functions, or classes anything. but functions would be preferred, since you could call them again, and again.

def menu(): # Code to menu

like that. make your menu stuff there, and call the name where you want it.

menu()

like that.
It should work

Profile icon
Wumi4

For this, you can use functions, it can reuse it every time you call it!

# The menu function def menu(): # Your code here # Every time you want to call it, # just do: menu() # Easy! right?

Methods are a term from classes, it's basically functions but it's scope is only inside the class and not globally like functions. To call it, you will need to call the class name, then a ., and finally the method name:

# A simple example of classes in Python class Breakfast: def serve(who): print("Have a nice breakfast, " + who + "!") # Use it is like this: Breakfast.serve("John") # Output: # Have a nice breakfast, John!

You should learn more about Python classes to know more about it!