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