How do you make a menu in python
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.
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!
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!