2

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
  • 2
    I think L1 would make more sense being a dict so you can label each value, e.g. `L1 = {"Units":int(units),"Revenue":int(rev)} etc. etc.` – JeffUK Jun 22 '21 at 15:15

4 Answers4

1

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 ...
JeffUK
  • 4,107
  • 2
  • 20
  • 34
Salem
  • 257
  • 3
  • 13
1

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 ;-)

Cyrille Pontvieux
  • 2,356
  • 1
  • 21
  • 29
0

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)
ShiraStar
  • 96
  • 3
0

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)
Sarah Messer
  • 3,592
  • 1
  • 26
  • 43