When they invented programming functions, it’s because they’re
MUCH BETTER to organize the program and its contents -
and this includes organizing the menu structure: you should not think
that only the functionalities at the tips should be functions and that
any code in the main body, either way, serves to
do the menus.
If the menus all behave in the same way: have to display a list of options, pick a number
and perform something else based on that number, the best thing you do is create a
generic function to display these menus and the answer - and then you get the whole structure pre
of the program very short and separate from the main logic:
aeronaves = []
aeroportos = []
def menu(titulo, opcoes):
while True:
print("=" * len(titulo), titulo, "=" * len(titulo), sep="\n")
for i, (opcao, funcao) in enumerate(opcoes, 1):
print("[{}] - {}".format(i, opcao))
print("[{}] - Retornar/Sair".format(i+1))
op = input("Opção: ")
if op.isdigit():
if int(op) == i + 1:
# Encerra este menu e retorna a função anterior
break
if int(op) < len(opcoes):
# Chama a função do menu:
opcoes[int(op) - 1][1]()
continue
print("Opção inválida. \n\n")
def principal():
opcoes = [
("Adicionar", adicionar),
("Listar", listar),
("Procurar", procurar)
]
return menu("SA AIRLINES", opcoes)
def adicionar():
opcoes = [
("Aeronaves", adicionar_aeronave),
("Aeroportos", adicionar_aeroporto),
# ...
]
return menu("Adicionar", opcoes)
def adicionar_aeronave():
aeronaves.append(input("Nova aeronave: "))
def adicionar_aeroporto():
aeroportos.append(input("Nova aeronave: "))
#...
def listar():
...
def procurar():
...
principal()
I used an important feature of Python which is the possibility to pass functions as normal objects - that is, the "menu" function receives the functions themselves that should call, in each option, with parameters - and calls the appropriate functions in the line opcoes[int(op) - 1][1]()
.
The functions that use the menu define the options as a sequence of items with two other internal items: the first is the printable name of the option, and the second is the function itself, as an object. For this just put the name of the function without adding the ( )
that characterize the function call. (Otherwise Python simply calls the function unconditionally at that point and adds the value returned by it in the list of options)
Thus, the line opcoes[int(op) - 1][1]()
"says" in item "op - 1" of the options , take the second item - which is the function itself (this we declare within the various functions that declare options to call the menu
) and call this object as a function, without parameters.
Note that doing so, if tomorrow you decide to add a graphical interface for example instead of using prints and inputs, just redo the "menu" function and the whole structure of the program continues working normally.
You should also put what you have and/or an example of the final result you want. You can edit here: http://answall.com/posts/172820/edit
– Miguel
I’ve already posted the code I have, I think from here I can get an idea of what I was trying to do
– SK1L3D
Hello SKL13D, one option is to have dictionaries representing each level menu. The value of each key is either a function (which you can call for) or another dictionary (representing another level of menus).
– Anthony Accioly
Do you need the while there? Why?
– Miguel
it was to re-display the menu until the user wants to go back to the previous one or leave the program anyway, but I think with the hint that Anthony gave I can put the menu right, I’ll see.
– SK1L3D
Use functions. Imagine that the function does not need to know where it was called - Panneas displays its options and returns the response. And then you realize that you don’t need to have any difference of functions that display sub-menus and those that effectively do things of the system.
– jsbueno