Problem with menu in python

2

[EDITED QUESTION] Hi, how are you? I hope they look good. I have a problem with the menu and sub-menu in Python. I'll explain to what I want to get, I want to make a main menu which will have its sub-menu and that sub-menu will have another sub-menu and so on.

  

When I'm in a sub-menu and I want to go back to the previous menu. Then it gets stuck when you re-sign an option

CODE IN EXECUTION

Selecciona una opción
    1 - ABML
    2 - Alta Paciente
    3 - Manejo de turnos
    9 - salir
inserta un numero valor >> 1
Selecciona una opción
    1 - Pacientes
    2 - Medicos
    3 - Habitaciones
    9 - salir
inserta un numero valor >> 9
Selecciona una opción
    1 - ABML
    2 - Alta Paciente
    3 - Manejo de turnos
    9 - salir
inserta un numero valor >> 1

Selecciona una opción
    1 - Pacientes
    2 - Medicos
    3 - Habitaciones
    9 - salir
Selecciona una opción
    1 - ABML
    2 - Alta Paciente
    3 - Manejo de turnos
    9 - salir
inserta un numero valor >>

That would be once executed as you see the main menu consists of (ABML, High patient and shift management) insert the value 1 to go to the menu-ABML until everything is perfect, from the menuABML choose option 9 to return to the main menu (And come back!) but when I want to go back to the menu ABML (ERROR! the menu ABML and the main menu are printed simultaneously)

Here I leave the code of Menu.py (main) and Menu_ABML.py (menu abml)

Menu.py

import os

def menu():
    os.system('cls')
    print ("Selecciona una opción")
    print ("\t1 - ABML")
    print ("\t2 - Alta Paciente")
    print ("\t3 - Manejo de turnos")
    print ("\t9 - salir")

while True:
    opcionMenu = 0

    # Mostramos el menu
    menu()

    # solicituamos una opción al usuario
    opcionMenu = input("inserta un numero valor >> ")

    if opcionMenu=="1":
        from Menu_ABML import *
        print ("")
        menuABML()

    elif opcionMenu=="2":
        print ("")
        input("Has pulsado la opción 2...\npulsa una tecla para continuar")
    elif opcionMenu=="3":
        print ("")
        input("Has pulsado la opción 3...\npulsa una tecla para continuar")
    elif opcionMenu=="9":
        break
    else:
        print ("")
        input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

Menu_ABML.py

import os

def menuABML():
    os.system('cls')
    print ("Selecciona una opción")
    print ("\t1 - Pacientes")
    print ("\t2 - Medicos")
    print ("\t3 - Habitaciones")
    print ("\t9 - salir")

while True:
    # Mostramos el menu
    menuABML()

    # solicituamos una opción al usuario
    opcionMenu = input("inserta un numero valor >> ")

    if opcionMenu=="1":
        from Menu_Pacientes import *
        print ("")
        menuPACIENTES()

    elif opcionMenu=="2":
        from Menu_Medicos import *
        print ("")
        menuMEDICOS()

    elif opcionMenu=="3":
        from Menu_Habitaciones import *
        print ("")
        menuHABITACIONES()

    elif opcionMenu=="9":
        from Menu import *
        print ("")
        Menu()

    else:
        print ("")
        input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

Since I thank you very much, I am a beginner in this at the moment and I am very helpful your collaboration.

    
asked by Gerónimo Membiela 28.12.2018 в 02:54
source

1 answer

1

Explanation:

Basically the error occurs because when you give it 9, Menu() is executed, but also the cycle continues, causing MenuABL() to be executed again.

To clarify:

elif opcionMenu=="9":
   from Menu import *
   print ("")
   Menu()

That piece of code executes Menu() , but does not close the cycle, so the cycle goes to the next iteration that starts with:

# Mostramos el menu
menuABML()

And execute menuABL() .

That's why you're seeing Menu and menuABL twice.

Solution:

You have to stop the execution of the cycle once you return to Menu , otherwise you will execute MenuABL again, for this add a simple break , thus leaving the code of Menu_ABML .py :

import os

def menuABML():
    os.system('cls')
    print ("Selecciona una opción")
    print ("\t1 - Pacientes")
    print ("\t2 - Medicos")
    print ("\t3 - Habitaciones")
    print ("\t9 - salir")

while True:
    # Mostramos el menu
    menuABML()

    # solicituamos una opción al usuario
    opcionMenu = input("inserta un numero valor >> ")

    if opcionMenu=="1":
        from Menu_Pacientes import *
        print ("")
        menuPACIENTES()

    elif opcionMenu=="2":
        from Menu_Medicos import *
        print ("")
        menuMEDICOS()

    elif opcionMenu=="3":
        from Menu_Habitaciones import *
        print ("")
        menuHABITACIONES()

    elif opcionMenu=="9":
        from Menu import *
        print ("")
        Menu()
        break # ----> aquí lo importante

    else:
        print ("")
        input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

Finally, and as a recommendation, read this question and change the imports at the beginning of the program as well as import os , it is not very good to import them into a cycle.

Update

Try the following:

Menu.py

import os
from Menu_ABML import show_menu_ABML

def menu():
    os.system('cls')
    print("Selecciona una opción")
    print("\t1 - ABML")
    print("\t2 - Alta Paciente")
    print("\t3 - Manejo de turnos")
    print("\t9 - salir")

def show_main_menu():
    while True:
        opcionMenu = 0

        # Mostramos el menu
        menu()

        # solicituamos una opción al usuario
        opcionMenu = input("inserta un numero valor >> ")

        if opcionMenu == "1":
            print("")
            show_menu_ABML()

        elif opcionMenu == "2":
            print("")
            input("Has pulsado la opción 2...\npulsa una tecla para continuar")
        elif opcionMenu == "3":
            print("")
            input("Has pulsado la opción 3...\npulsa una tecla para continuar")
        elif opcionMenu == "9":
            break
        else:
            print("")
            input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

if __name__ == '__main__':
    show_main_menu()

Menu_ABML.py

import os

def menuABML():
    os.system('cls')
    print("Selecciona una opción")
    print("\t1 - Pacientes")
    print("\t2 - Medicos")
    print("\t3 - Habitaciones")
    print("\t9 - salir")

def show_menu_ABML():
    while True:
        # Mostramos el menu
        menuABML()

        # solicituamos una opción al usuario
        opcionMenu = input("inserta un numero valor >> ")

        if opcionMenu == "1":
            print("Menú de los pacientes")

        elif opcionMenu == "2":
            print("Menú de los médicos")

        elif opcionMenu == "3":
            print("Menú de alguien")

        elif opcionMenu == "9":
            print("")
            break

        else:
            print("")
            input("No has pulsado ninguna opción correcta...\npulsa una tecla para continuar")

That code as it is does what you want, basically what I did was put the cycles inside functions so they can be called at any time.

If they are not within the function and their scope is global, they will only be executed twice:

  • When running the file that contains them
  • When importing the file that contains them (this is the reason for what it says at the end of your first comment)

More recommendations :

In the ABML cycle do not call Menu() because it is the same function that called the ABML cycle, and this after a while will generate an error because the stack will reach its limit, an example of this situation:

- Execution of Menu() - > you enter the program

----- Execution of ciclo de ABML - > ABML is chosen

-------- Running Menu() - > you choose to leave

----------- Execution of ciclo de ABML - > ABML is chosen again

-------------- Running Menu - > you choose to leave again

----------------- ...... and so on until the program ends

And in the end the stack will have many functions, that will be occupying resources and obviously it is bad for the performance. Ideally, do the following:

- Execution of Menu() - > you enter the program

------ Execution of ciclo ABML - > ABML is chosen

------ Ends ciclo ABML - > you choose to leave

- Continue the cycle of Menu()

------ Execution of ciclo ABML - > ABML is chosen again

------ Ends ciclo ABML - > you choose to leave again

- Continue the cycle of Menu()

------ ..... and so on until the program ends

In this way, the stack is no longer as loaded as the previous form.

This last form is implemented in the update.

Finally, I recommend you to do your python programs in a modular way, almost all of them are done that way, read this

    
answered by 28.12.2018 / 10:47
source