-
import MenuBuilder
classs
from mr-menu.generator import MenuBuilder
-
create functions for Add
, Sub
, Multiply
and divide
def add():
num1 = int(input("enter num 1: "))
num2 = int(input("enter num 2: "))
return num1 + num2
def sub():
num1 = int(input("enter num 1: "))
num2 = int(input("enter num 2: "))
return num1 - num2
def mult():
num1 = int(input("enter num 1: "))
num2 = int(input("enter num 2: "))
return num1*num2
def div():
num1 = int(input("enter num 1: "))
num2 = int(input("enter num 2: "))
return num1/num2 if num2 != 0 else 0
-
create MenuBuilder
class object
builder = MenuBuilder()
-
Add the first menu (main menu)
builder.add(
identifier="main",
menu={1: "Add two nums", 2: "Sub two nums", 3: "More Options"},
functions={1: add, 2: sub, 3: None},
go_back_index=None,
)
-
Add the sub-menu
builder.add_submenu(
parent_identifier="main",
parent_menu_index=3,
submenu_identifier="main-submenu-1",
submenu={1: "Multiply two nums", 2: "Divide two nums"},
go_back_index=None,
replace_if_exist=True,
)
-
Handle the menu and submenu
If the user chooses option 1 (Add two nums), the handler
will ask for two inputs and return the sum of the numbers. But when the user chooses 3rd option in the main menu, The new sub-menu will be displayed. The user can then choose Multiply
and Divide
.
result = builder.handler(
prompt="Enter your choice:",
post_execution_label="The Task executed Successfully",
return_execution_result=True,
)