This is a question about some code I've written that works, but I'm not sure it's good code.
I would like to get some comments on this or if possible hints to build a better solution.
Background:
This is a small program to calculate some data, connect these to a participant and store it in a JSON file.
Here the already working solution:
menu_entries = (
('Order Pizza', order_pizza),
('Order Burger', order_burger),
('Order Coke', order_coke, '0.5')
)
def menu(menu_title, menu_entries):
while True:
mpoint = 0
print(menu_title)
# Build the menu entries plus one to close
for entry in menu_entries:
mpoint += 1
print(str(mpoint) + '.', entry[0])
print(str(mpoint + 1) + '.', 'Ende')
# Get value and validate
try:
task = int(input('> '))
except ValueError:
task = None
if task is None or task > mpoint + 1:
task = None
continue
if task == mpoint + 1:
print('Done')
break
# Build a function call
get_task = menu_entries[task - 1][1]
try:
get_task(menu_entries[task - 1][2])
except IndexError:
get_task()
def order_pizza(size=None):
if size is None:
size = ''
print('Bring me a Pizza!')
def order_burger(size=None):
if size is None:
size = ''
print('One {} Burger please.'.format(size))
def order_coke(size=None):
if size is None:
size = ''
print('A {} Coke please.'.format(size))
menu('Main menu', menu_entries)
As you can see the menu function gets a main title and a tuple of menu entries.
In these menu entries I could define the menu point title, a function name and one parameter.
All these other functions are only examples and are of no interest (they are ugly, I know).
Please have a look, criticize and if possible show me a better solution.