-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.py
More file actions
47 lines (41 loc) · 1.47 KB
/
menu.py
File metadata and controls
47 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# It's bad to use string literals everywhere so we
# willd define some constants to use instead
NAME = 'name'
FUNC = 'func'
class Menu:
# Class constructor in python
def __init__(self, title):
# title of our menu
self.title = title
# Our dictionary of dictionaries
self.menuData = {}
# Total number of items in menu
self.itemsInMenu = 0
def addMenuItem(self, name, func):
# Menu number is the length of our menuData
# dictionary plus one
menuNumber = len(self.menuData) + 1
self.menuData[str(menuNumber)] = { NAME: name, FUNC: func }
def run(self):
while True:
print ()
print ()
print ("*** " + self.title + " ***")
print ()
for menuNum, menuItem in self.menuData.items():
print(menuNum + ' - ' + menuItem[NAME])
print("x - exit")
print()
userInput = input(": ")
if userInput == 'x':
break
#Select the right item from our menu or "false"
#if it's not there
selected = self.menuData.get(userInput, False)
# User selected item that wasn't in our list
if not selected:
input("INVALID INPUT -- press enter to continue")
else:
#Run the function that was selected
selected[FUNC]()
input("Complete -- press enter to continue")