import os
# Agregar artículos
def agregar_articulo():
archivo_lista = open("lista.txt", "a")
articulo = input("Ingrese un artículo a la lista: ")
archivo_lista.write("{}\n".format(articulo))
archivo_lista.close()
# Ver los artículos
def ver_articulos():
mostrar_archivo = open("lista.txt", "r")
print(mostrar_archivo)
mostrar_archivo.close()
# Borra el archivo que contiene la lista de compras
def borrar_lista():
os.remove("lista.txt")
def compra_app():
# UI
print("#######Bienvenido a COMPRAPP###########")
eleccion = print(input("Qué desea hacer: \n 1- Agregar un artículo \n 2- Ver los articulos agregados"
"\n 3- Borrar la lista \n 4- Salir "))
while eleccion != 4:
if eleccion == 1:
agregar_articulo()
elif eleccion == 2:
ver_articulos()
elif eleccion == 3:
borrar_lista()
# Salir
print("#######Gracias y hasta la proxima!######")
compra_app()
It also does not show any errors.
You have two errors mainly, the first one is on the line:
eleccion
is the return ofprint
andprint
always returnsNone
. You don't needprint
to use this line at all,input
it already prints the string passed to it as an argument when it asks for user input. Sinceeleccion
it isNone
the condition ofwhile
is always true (None != 4
), so it enters the loop, but sinceeleccion
it is not either1
,2
or3
does not enter any of the conditions. By not modifying the value ofeleccion
within the loop itself ,while
you enter an infinite loop.For your code the line should be:
The other mistake is that
input
you must ask for it within thewhile
as already mentioned. It also doesn't hurt to validate the input in case the user doesn't enter a valid option, for example: