End while loop with break, but still in infinite loop

3

I'm starting in Python and programming in general and now that I had a few notes in hand, I started writing a simple program that consists of a menu with different options.

The first consists simply of a loop that allows you to enter ages in a list.

The problem I have is that after asking for confirmation in the loop to exit the break it does not cause the condition to be false and end the loop. I have tried with return False and the result is the same, I do not finish the loop to invoke the function menu() and return to the beginning.

while True:                             # bucle para introducir edades en la lista
    valor = input("Introduce edad: ")   # pedir valor por teclado
    listaedad.append(valor)             # añadir valor a listaedad
    confirm = input('Quieres seguir? ') # confirmación para seguir con el bucle

    if confirm in ('s', 'si', 'Si', 'SI'):      # si confirm es igual a los valores del IN
        continue                                # continua en el while
    elif confirm in ('n', 'no', 'No', 'NO'):    # si confirm es igual a los valores de IN
        print("Saliendo...")                    # imprime mensaje de salida
        break                                   # sal del bucle
    else:                                       # si confirm es cualquier otro valor distinto de SI/NO
        print('Opción no reconocida')           # imprime mensaje
        print('Saliendo...')                    # imprime mensaje
        break                                   # sal del bucle
return listaedad                        #retorna la lista
menu()              # vuelve al menu de inicio
    
asked by Montanyero Tux 26.08.2016 в 14:55
source

2 answers

0
  

return listaedad

It is not necessary to return the age list value. "Return" is used for a function to return a value. If the variable agelist is declared above as global you can see its contents whenever you want. So you can see it better, I'll give you the code as an example:

import os


listaedad = list ();

def limpiarPantalla():
    os.system('cls' if os.name == 'nt' else 'clear')

def menu ():
    print ('''
    Elige una opción:

        1. Añadir edad a la lista
        2. Ver lista de edades
        3. Salir del programa

        ''')

    opcion = input ('')
    return opcion



while True:
    limpiarPantalla ()
    opcion_elegida = menu ();
    limpiarPantalla ()
    if opcion_elegida == "1":

        while True:                             # bucle para introducir edades en la lista
            valor = input("Introduce edad: ")   # pedir valor por teclado
            listaedad.append(valor)             # añadir valor a listaedad
            confirm = input('Quieres seguir? ') # confirmación para seguir con el bucle

            if confirm in ('s', 'si', 'Si', 'SI'):      # si confirm es igual a los valores del IN
                pass                           # continua en el while
            elif confirm in ('n', 'no', 'No', 'NO'):    # si confirm es igual a los valores de IN
                print("Saliendo...")                    # imprime mensaje de salida
                break                                  # sal del bucle
            else:                                       # si confirm es cualquier otro valor distinto de SI/NO
                print('Opción no reconocida')           # imprime mensaje
                print('Saliendo...')                    # imprime mensaje
                break                                   # sal del bucle

    elif opcion_elegida == "2":

        for i in listaedad:
            print (i)
        print ("Pulsa cualquier tecla para volver al menu")
        input ('')


    elif opcion_elegida == "3":
        break

    else:
        print ("Opción no disponible")
    
answered by 26.08.2016 / 16:30
source
0

return is used within functions or methods. Is this loop while part of a function? If so, please add the complete code.

If the while is not inside a function or method if you delete the return it should work correctly.

If the loop while is part of the function you can put the return instead of the break s so that it returns listaedad just at the moment when the while stops being fulfilled.

    
answered by 26.08.2016 в 16:14