Do not save python list

0

Hello, I am making a shopping cart system and when asking me to return the purchase information, it does not store all the values entered, it only delivers the last value entered.

def opc1():
    global lis
    lis=[]
    s="si";
    while(s=="si" or s=="SI"):
        print("---------------Lista de Productos--------------");
        cont=1;
        for i in lis:
            print(cont,".-nombre : ",i[0],"     $",i[1]);
            cont=cont+1
        else:
            nombre=input("Introduzca nombre producto : ");
            valor=int(input("Introduzca valor del producto : "));
            s=input("¿Desea ingresar un nuevo producto? si - no : ");
            lis=lis + [[nombre,valor]]
    ing();

The rest of the code is as follows: Hi, thanks for your answer, my problem is that when you arrive at the shopping cart you do not store all the items entered, only the last thing you entered.

def opc2():
global v;
v=[]
print("");
print("            *****REALIZAR VENTA*****");
print("");
print("---------------Lista de Productos--------------");
cont=1;
f   print("");
nom=int(input("Introduzca numero de producto a comprar : "));
can=int(input("Introduzca cantidada comprar de dicho producto : "));
res=nom-1;
cans=lis[res][1]*can;
print("");
print("--------------------------------------------");
print("");
print("******Carrito de compra*****");
print("");
print("---------------Lista de Productos--------------");
cont=1
print("");

and I think the fault I have is in this section of code:

print(cont,".-",lis[res][0],"Cantidad : ",can,"$",cans );
v= [lis[res][0],cans+cans]
print(v);
print("");
print("--Acciones--");
print("");
print("Presione S para seguir comprando");
print("");
print("Presione numero de producto que desea quitar");
print("");
p=input("introduzca opcion de acuerdo cuatro anterior");
if(p=="s" or p=="S"):
    opc2()





ing(); 

    
asked by Regy 26.06.2017 в 23:38
source

1 answer

1

The code you provide is correct. Keep in mind that every time you call opc1 the variable lis becomes an empty list because you do: lis=[] at the beginning of the function. This causes any content of lis to be erased each time you execute it. Remove this line so that the list is not deleted each time you call the function. If this is not your problem, you should add the rest of the code and explain the problem in more detail.

A few observations in case you are interested:

  • On the other hand, it is not necessary to use ; to finish an instruction in Python.
  • You can use the append method in the lists instead of concatenating if you wish.
  • It is not necessary to use global in this case.

The code should be the following:

def opc1():
    s="si"
    while(s=="si" or s=="SI"):
        print("---------------Lista de Productos--------------")
        cont=1
        for i in lis:
            print(cont,".-nombre : ",i[0],"     $",i[1])
            cont=cont+1
        else:
            nombre=input("Introduzca nombre producto : ")
            valor=int(input("Introduzca valor del producto : "))
            s=input("¿Desea ingresar un nuevo producto? si - no : ")
            lis.append([nombre,valor])

    ing()

Execution example:

 >>> lis = []
 >>> opc1()

 ---------------Lista de Productos--------------
 Introduzca nombre producto : cebolla
 Introduzca valor del producto : 100
 ¿Desea ingresar un nuevo producto? si - no : si
 ---------------Lista de Productos--------------
 1 .-nombre :  cebolla      $ 100
 Introduzca nombre producto : zanahoria
 Introduzca valor del producto : 200
 ¿Desea ingresar un nuevo producto? si - no : no

 >>> print(lis)
 [['cebolla', 100], ['zanahoria', 200]]

 >>> opc1()

 ---------------Lista de Productos--------------
 1 .-nombre :  cebolla      $ 100
 2 .-nombre :  zanahoria      $ 200
 Introduzca nombre producto : helado
 Introduzca valor del producto : 300
 ¿Desea ingresar un nuevo producto? si - no : no

 print(lis)
 [['cebolla', 100], ['zanahoria', 200], ['helado', 300]]
    
answered by 27.06.2017 в 00:03