Cycle with variable dependent on another variable Python


I put them in context, I have the following code:

opcion = int(input("ingrese mesa: "))
if opcion == 1:
    opcion2 = int(input("ingrese plato (0 para terminar): "))
        while opcion2 != 0:
            x = opcion2 - 1
            c6 += 1
            opcion2 = int(input("ingrese plato (0 para terminar): "))

And I need a function that makes a summation of all the prices that were added to the mesa6 list that is within the last while , the truth is that I do not have many ideas, but I had thought about doing a for cycle that would go through the mesa6 list in a range of the variable c6 but given that I have to do it in a general for 6 different list I do not know how to formulate it.

They will understand better seeing the complete code, I already had problems for the append to the table [x] list since I wanted it to be a function that could vary according to the variable x but I did not know how, so my code got a little big with so many if :


asked by Victor Caicedo 22.12.2018 в 22:27

1 answer


If I'm not wrong you're iterating a tripla and you have to add the prices that could be solved in the following way

suma = 0
for i in mesa:
    suma += int(i[1])

Here you are defining the sum and then iterating the triplas to add the prices of the table, but to do this in all the tables I recommend driving with classes

class Mesa:
    def __init__(self):
        self.numero = 0
        self.names = []
        self.prices = []
        self.types = []
        self.names = ["Papas", "Arroz"]
        self.prices = ["200", "2000"]
        self.types = ["tipo 1", "tipo 2"]
    def agregar_info(self):
        option = int(input("Put table: "))
        option2 = 210
        while option2 != 0:
            self.numero = option
            option2 = int(input("Put the id of food (0 to end): "))
            x = option2 - 1

    def cuenta(self):
        suma = 0
        for i in self.prices:
            suma += int(i)
        return suma

#Hacer la interfaz en consola

#Agregar datos

mesa = Mesa()

I hope I have helped you, regards

answered by 28.12.2018 в 03:40