Re-execute a part of the code until it is fulfilled

1

Good friends, I have a doubt, the fact is that I am new to programming and I am learning python 3, I found a problem practicing the code of the true a very basic program. What happens is the following. I want to continue executing the program until they give me a valid option. You will see more clearly when you see the code

print("Bienvenido, escoja una opcion. \n1)Mostrar el contenido de la 
agenda.\n2)Añadir contacto a agenda.\nUtilize el numero correspondiente para 
seleccionar la accion.")

opcion=int(input(">"))

if opcion==1:

    print("Ha seleccionado la opcion 1.")
    agenda=open("agenda.csv")
    print(agenda.read())
    agenda.close()
elif opcion==2:
    agenda=open("agenda.csv", "a")
    nombre=input("Introduzca el nombre\n>")
    numero=input("Introduzca el numero\n>")
    agenda.write(nombre)
    agenda.write(",")
    agenda.write(numero)
    agenda.write("\n")
    print("Contacto guardado con exito")
    agenda.close()
else:
    print("Ha escogido una opcion invalida, intentelo de nuevo.")

What is happening to me is the following, if we enter a different option than 1 or 2, it goes to the else and the program ends. But as I can ask once a time for a valid option and when they give me one that is really valid, execute the correct code and finish the program, not like when it arrives at the else and ends, rather than ask as many times as necessary, giving the message error and new value for the option variable

    
asked by limg21 07.05.2017 в 00:34
source

2 answers

0

The simplest option is to enclose the code in an infinite cycle ( while True ) and break it ( break ) when a valid option is inserted:

opcion = None
while True:
    opcion=int(input(">"))
    if opcion in (1,2):
        break
    else:
        print("Ha escogido una opcion invalida, intentelo de nuevo.")

However, in this type of exercises, what is usually done is to allow after doing an action to do something else without leaving the program, for example to see the agenda and then add a contact. For this, it is enough to do something like:

menu ='''
Escoja una opción:
    1)Mostrar el contenido de la agenda.
    2)Añadir contacto a agenda.
    3)Salir.
Utilize el numero correspondiente para seleccionar la accion.
> '''

print('Bienvenido a la agenda de contactos')

while True:
    opcion=input(menu)

    if opcion=='1':
        print("Ha seleccionado la opcion 1.")
        agenda=open("agenda.csv")
        print(agenda.read())
        agenda.close()

    elif opcion=='2':
        agenda=open("agenda.csv", "a")
        nombre=input("Introduzca el nombre\n>")
        numero=input("Introduzca el numero\n>")
        agenda.write(nombre)
        agenda.write(",")
        agenda.write(numero)
        agenda.write("\n")
        print("Contacto guardado con exito")
        agenda.close()

    elif opcion == '3':
        print('Hasta pronto...')
        break

    else:
        print("Ha escogido una opcion invalida, intentelo de nuevo.")

It's just an example, it can be improved as desired, for example by cleaning the console / terminal after each event.

    
answered by 07.05.2017 / 01:17
source
0

I do not have Python3, but this should work:

idf = 10
while idf == 10:
print("Bienvenido, escoja una opcion. \n1)Mostrar el contenido de la 
agenda.\n2)Añadir contacto a agenda.\nUtilize el numero correspondiente para 
seleccionar la accion.")

opcion=int(input(">"))

if opcion==1:

    print("Ha seleccionado la opcion 1.")
    agenda=open("agenda.csv")
    print(agenda.read())
    agenda.close()
elif opcion==2:
    agenda=open("agenda.csv", "a")
    nombre=input("Introduzca el nombre\n>")
    numero=input("Introduzca el numero\n>")
    agenda.write(nombre)
    agenda.write(",")
    agenda.write(numero)
    agenda.write("\n")
    print("Contacto guardado con exito")
    agenda.close()
else:
    print("Ha escogido una opcion invalida, intentelo de nuevo.")
    
answered by 07.05.2017 в 00:59