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.
Voters
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!
When it comes to menus, this is what I do:
I Hope this helps!
@Squirrel777 but for that, you would need elif and not else if