lista_de_productos=[['Linea','ID','Descrpcion',' Cantidad','Precio'],
[1,1,'Agua de 1 litro ',100,15],
[2,2,'Leche 1 litro ',100,25],
[3,3,'Manzana ',100,5],
[4,4,'Cereal ',100,30],
[5,5,'Agua de 2 litros',100,28]]
b=''
for i in range(6):
for j in range(5):
b+=str(lista_de_productos[i][j])+'\t'
print (b)
b=''
I have this code where I have an array and display it as a list, how can I get the user to add products?
Hello friend, if you want to add data in an array, what you can do is the following, I will give you an example:
In this first example what we do with the append method is to add a new array to our array so the result of the array is expanded with a new array.
In this second example, what you do is locate the position of the matrix and add new information to that row.
Also if you don't want to use it this way you can use numpy with its own append method and it would look something like this:
Finally, with this last example we have two arrays and what you do with numpy is accommodate them by their numerical size and what is basically done is concatenate both arrys
I hope it helps you friend.
Based on your code, I propose the following solution:
Assumptions:
Step 1: We request the information from the user
We start by asking the user for the number of products they want to add, since that is how we define the number of rows that we must add to the list. For this we use the input() function, which is used so that the user can type some text. Since, by default, this function is of type "string" and we need the user to give us a number, we tell Python to convert what the user has typed into an "int" so that it can be read further. forward as a number.
Now, since we need information for each row, we do a "for i in range()" that asks the user for the description of the product, the quantity and the price. It is not necessary to ask for the information for the 'Line' and 'ID' columns as it can be deduced, using the len() function which gives us the number of elements in a list.
Step 2: We add the data to the list
For this, the append() method is needed, which adds the elements that you indicate to the end of the list.
Step 3: Show the list
Here it is just putting what you had, the only thing that changes is the number of rows since this number increased when the user added their new products.
Step 4 (Optional): Enhance List Display
In the initial list, for each product in the "Description" column, I see that you manually added a few blank spaces at the end so that the columns look nicely aligned. I'd suggest removing them and having Python add them automatically. For that I come up with the following:
First you need to know which row has the longest text string in the "Description" column and how big that text string is. For that, we set a variable to 0 ("max_size_description") and do a "for i in range()" that checks the entire list (specifically, that checks the element in position 2 of each row). At the end, the variable max_size_amount takes as its value the number of characters in the longest text string in the entire list. The variable "indice_description", for its part, is responsible for indicating in which row that longest string is found.
With the above, we can now tell Python to add the whitespace. What this "for i in range" does is measure the size of the string in position 2 of each row and compare it to the size of the longest string. Thus, the difference between these two is the number of spaces that must be added to the end of the string so that, visually, each element appears below its respective column.
We do the same for the next column:
Once you've done this, just copy the code from Step 3 again to see how the list turned out.