How to add elements from a matrix in python

0

I have to carry out a shipment management program, the program asks me to save the arrival of trucks, for it asks me to keep a truck that is numbered from 1 to 3 according to the type of truck 1 = tanker 2 = box 3 = platform and put the number of trucks that have arrived, that is, 5 tanker trucks have arrived. The problem that I have is that he asks me to add the trucks that are arriving from the same type, if I first put 5 tankers and then 5 more than that there are 10 tankers. I keep the data in a matrix and my problem is how can I go through the matrix so that I can add the different trucks as they arrive.

Exercise question: The user will be asked for the type of truck that has arrived (1 = Platform, 2 = Tank or 3 = Box) and how many trucks (between 1 and 10). Add the new trucks to those that were already available. Ask if we want to introduce another truck arrival or go to the main menu.

Here my code.

def llegadaCamion():
   while True:
     nuevos_Camiones=[]
     input("Has pulsado la opción de Llegada de camion  ...\npulsa una tecla para continuar")
     #input donde capturamos los datos que vamos a pedir al camion
     tipoCamion=(input("Introduce el tipo de camion: 1=Plataforma, 2=Tanquero, 3=Caja :"))
     #ahora introducimos el campo que tenemos en el array utilizando append
     nuevos_Camiones.append(tipoCamion)
     cantidadCamiones=int(input("Introduce el numero de camiones que han llegado, maximo 10 camiones: "))
        #Vamos a crear una condición para que nos avise si introducimos mas de 10 camiones 
     if cantidadCamiones <=10:
        nuevos_Camiones.append(cantidadCamiones)
        lista_llegada_camiones.append(nuevos_Camiones)
        print(lista_llegada_camiones[:])
     else:
        print("Estas introduciendo mas de 10 camiones!!!!!!")
        cantidadCamiones=int(input("Introduce el numero de camiones que han llegado, maximo 10 camiones: "))
        nuevos_Camiones.append(cantidadCamiones)
        lista_llegada_camiones.append(nuevos_Camiones)

     opcion_terminar=(input("Si quieres introducir un nuevo camion pulsa 1, en caso de volver al menu principal pulsa 2: "))
     if opcion_terminar =="1":
        llegadaCamion()
     elif opcion_terminar =="2":
            break
    
asked by David 10.05.2018 в 14:03
source

1 answer

1

I hope it serves you, your limit condition of 10 trucks seems a little fragile, that part I leave as a reflection.

It is worth remembering that defaultdict creates dictionaries with initial values already defined, it is basically a safe way to create dictionaries when we do not know what all the keys will be.

from collections import defaultdict
def llegadaCamion():
   nc = defaultdict(int)
   while True:
     # Capturamos el tipo de camion
     tipoCamion=(input("Introduce el tipo de camion: 1=Plataforma, 2=Tanquero, 3=Caja :"))
     cantidadCamiones=int(input("Introduce el numero de camiones que han llegado, maximo 10 camiones: "))

     # Limitamos el número de camiones a 10   
     if cantidadCamiones <=10:
        nc[tipoCamion] += cantidadCamiones
     else:
        print("Estas introduciendo mas de 10 camiones!!!!!!")
        cantidadCamiones=int(input("Introduce el numero de camiones que han llegado, maximo 10 camiones: "))
        nc[tipoCamion] += cantidadCamiones

     opcion_terminar=(input("Si quieres introducir un nuevo camion pulsa 1, en caso de volver al menu principal pulsa 2: "))
     if opcion_terminar =="2":
            return dict(nc)

print(llegadaCamion())
    
answered by 12.05.2018 в 05:34