How can I gather information from each instance of a loop?

Viewed 55

Very new to python but I want to store each list generated from each time the loop runs the code again so I can use them in later parts of my code.

i = 1
while i <= 5:
    units = input("Insert Units Sold: ")
    rev = input("Insert Revenue of Sale: ")
    revper = round((int(rev) / int(units)))
    print("Revenue per unit: " + str(revper))
    cogs = (3 * int(units))
    print("Cost of Goods Sold: " + str(cogs))
    profit = (int(rev) - (cogs))
    print("Profit: " + str(profit))
    profitper = round((profit / int(units)))
    print("Profit per Unit: " + str(profitper))

    L1 = [int(units), int(rev), revper, cogs, profit, profitper]
    print("Line " + str(i) + str(L1))
    i = i + 1
4 Answers

Here is a way of doing it:

i = 1
myLists = []

while i <= 5:
    units = input("Insert Units Sold: ")
    rev = input("Insert Revenue of Sale: ")
    revper = round((int(rev) / int(units)))
    print("Revenue per unit: " + str(revper))
    cogs = (3 * int(units))
    print("Cost of Goods Sold: " + str(cogs))
    profit = (int(rev) - (cogs))
    print("Profit: " + str(profit))
    profitper = round((profit / int(units)))
    print("Profit per Unit: " + str(profitper))

    L1 = [int(units), int(rev), revper, cogs, profit, profitper]

    # append L1 to myLists
    myLists.append(L1)
    print("Line " + str(i) + str(L1))
    i = i + 1

Then you can loop on myLists to get each generated list using a for loop :

for list in myLists:
    # do smth ...

Your variable L1 will be overwritten at each loop iteration. But at the end, the last value will be kept in L1. If this what you want, then you already have it.

If you want to store all L1 values, store them in another list:

lst = []
i = 1
while i <= 5:
  ...
  L1 = [int(units), int(rev), revper, cogs, profit, profitper]
  lst.append(L1)
  ...
print(lst)

You can of course access each iteration with for instance lst[2] for the third instance.

by the way, it’s not in the coding convention to use capitalized letters for a variable. Those are reserved for classes and constants. So better use l1 instead of L1.

Hope it helps you and you will improve into your python skills ;-)

You can append the list to dictionary and access it by : all_lists['L1'] or all_lists['L2'] ...

And TIP for a beginner - use range instead of while :)

all_lists = dict()
for i in range(1, 6):
    units = input("Insert Units Sold: ")
    rev = input("Insert Revenue of Sale: ")
    
    revper = round((int(rev) / int(units)))
    print("Revenue per unit: " + str(revper))
    cogs = (3 * int(units))
    print("Cost of Goods Sold: " + str(cogs))
    profit = (int(rev) - (cogs))
    print("Profit: " + str(profit))
    profitper = round((profit / int(units)))
    print("Profit per Unit: " + str(profitper))
    l = [int(units), int(rev), revper, cogs, profit, profitper]
    print("Line", str(i), str(l))

    all_lists['L{0}'.format(i)] = l

print(all_lists)

I would use a collection of dicts:

from pprint import pprint

dict_dict = {}
i = 1
while i <= 5:
    units = input("Insert Units Sold: ")
    rev = input("Insert Revenue of Sale: ")
    revper = round((int(rev) / int(units)))
    print("Revenue per unit: " + str(revper))
    cogs = (3 * int(units))
    print("Cost of Goods Sold: " + str(cogs))
    profit = (int(rev) - (cogs))
    print("Profit: " + str(profit))
    profitper = round((profit / int(units)))
    print("Profit per Unit: " + str(profitper))

    L1 = {
        'Units sold': int(units),
        'Revenue': int(rev),
        'Revenue per unit': revper,
        'Cost of goods sold': cogs,
        'Profit': profit, 
        'Profit per unit': profitper
   }
   dict_dict[i]=L1
   print("Line " + str(i) + str(L1))
   i = i + 1

pprint(dict_dict)
Related