-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37_menu.py
30 lines (21 loc) · 1.01 KB
/
37_menu.py
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
# -*- coding: utf-8 -*-
"""
Specifically, write a new module called “menu” (in the file menu.py). The
module should define a function, also called menu. The function takes any
number of key-value pairs as arguments. Each value should be a callable, a
fancy name for a function or class in Python.
When the function is invoked, the user is asked to enter some input. If the
user enters a string that matches one of the keyword arguments, the function
associated with that keyword will be invoked, and its return value will be
returned to menu’s caller. If the user enters a string that’s not one of the
keyword arguments, they’ll be given an error message and asked to try again.
"""
def menu(**funcs):
while (user_input := input('>>')) not in funcs:
print(f'Error! Functions avaliable: {", ".join(funcs.keys())}')
print(f'Result is: {funcs[user_input]()!r}')
def func_a():
return 'This was the a function'
def func_b():
return 'This was the b function'
menu(a=func_a, b=func_b)