Python menu problems

0

My menu has many errors and I would like to know what I'm doing wrong. One of the problems is that it does not let me leave the program if I press 4, another is that if I choose an option it shows me the figure I want but then I do not know how to go back to the main menu if the user wants to continue asking ... change I get this error

I think here is the error, I do not know:

#programa
opcion=Opciones()
while True:
#------------------------------------------------------------------- 
#Si el usuario quiere pintar un cuadrado se le da multiples opciones   
if opcion == 1:
    print
    Cuadrado(6,6)
    eleccion=Menu_Colores()
    if eleccion == 1:
        Pintar_CuadradoG1(13,13)
        input()
    elif eleccion == 2:
        Pintar_CuadradoG2(13,13)
        input()
    elif eleccion == 3:
        Pintar_CuadradoG3(13,13)
        input()
    elif eleccion == 4:
        Pintar_CuadradoP1(4,4)
        input()
    elif eleccion == 5:
        Pintar_CuadradoP2(4,4)
        input()
    elif eleccion == 6:
        Pintar_CuadradoP3(4,4)
        input()
#-------------------------------------------------------------------
#Si el usuario quiere pintar un triangulo se le da multiples opciones             
elif opcion == 2: 
        Triangulo()
        seleccion = Menu_Colores()
        if seleccion == 1:
            Pintar_TriG1()
            input()
        elif seleccion == 2:
            Pintar_TriG2()
            input()
        elif seleccion == 3:
            Pintar_TriG3()
            input()
        elif seleccion == 4:
            Pintar_TriP1()
            input()
        elif seleccion == 5:
            Pintar_TriP2()
            input()
        elif seleccion == 6:
            Pintar_TriP3()
            input()

#---------------------------------------------------------------------
#Si el usuario quiere pintar un rombo se le da multiples opciones             
elif opcion == 3:
    Rombo(7)
    decision = Menu_Colores()
    if decision == 1:
        Pintar_RomboG1(13)
        input()
    elif decision == 2:
        Pintar_RomboG2(13)
        input()
    elif decision == 3:
        Pintar_RomboG3(13)
        input()
    elif decision == 4:
        Pintar_RomboP1(7)
        input()
    elif decision == 5:
        Pintar_RomboP2(7)
        input()
    elif decision == 6:
        Pintar_RomboP3(7)
        input()

#---------------------------------------------------------------------

#Si el usuario ingresa una opcion no valida        
else:
    print"Debe ingresar una opcion valida"
#---------------------------------------------------------------------
  opcion=Opciones()        
print 'Adios, nos vemos :3'
    
asked by Wolf 24.12.2018 в 12:43
source

1 answer

0

To begin with, I think the function Opciones is also involved, you wrote it so if you have not modified it:

def Opciones():
    op=0
    while op!=1 and op!=2 and op!=3 and op!=9:
        print
        print " Menu de opciones"
        print"-----------------------"
        print "[1] Rombo"
        print "[2] Cuadrado"
        print "[3] Rombo "
        print "[9] terminar la ejecucion"
        op = int(raw_input("ingrese opcion: "))
    return op

That said , I think the solution is to restructure the code. All those calls to input() without parameters could be replaced by something that really works. For example, this line:

opcion=Opciones()

Replacing all those input() empty and unassigned by opcion=Opciones() , at least the menu becomes more fluid, invoking the main menu after painting a figurine. It still has small shortcomings, but only with this already improves a lot.

Additional information

But EYE! I can not ignore that this adds more duplicate code To the mix. If your program code adheres more to the DRY principle ( Do not Repeat Yourself , or Do not Repeat ), it will be easier to keep the code in the future (add modifications, fix faults, etc.). To solve it, you would have to refactor. Now that, it is not easy to decide how to do that refactoring.

This section of refactoring.guru (in English) explains in detail about the problems that duplicate code can give you, and how to avoid it. I strongly recommend you take a look at the pages refactoring.guru and sourcemaking.com . Both contain very complete information (in English, but very good) on refactoring techniques, as well as design patterns and how to apply them, as well as anti-patterns, "smells" of the code and how to counteract them, etc. The information shown in them is valid for multiple programming languages, and is a great help to learn the Art of Clean Code.

After seeing that, you should judge yourself how you prefer to restructure your code, I think. But, to say something, maybe it would be beneficial for you to replace the magic numbers by symbolic constants ; would favor a more obvious code in the eyes of a human. Although, even with the use of symbolic constants, it "smells" certain obsession with primitive types .

Neither do you need to refactor like crazy! But I think it would be good for you to consider all that information.

    
answered by 24.12.2018 в 16:14