Now you try it!
Write some code that will count up the number of aluminum, paper, and plastic items in this list:
items = iter(["aluminum", "plastic", "plastic", "plastic",
"plastic", "paper", "plastic", "aluminum", "aluminum", "paper", "plastic"])
items = iter(["aluminum", "plastic", "plastic",
"plastic", "plastic", "paper", "plastic", "aluminum", "aluminum", "paper", "plastic"])
def countItems(recyclable, aluminums, papers, plastics):
"""count the items based on material"""
case (recyclable):
match "aluminum":
current = aluminums[0]
aluminums[0] = current+1
match "paper":
current = papers[0]
papers[0] = current+1
match "plastic":
current = plastics[0]
plastics[0] = current+1
else:
raise TypeError("item not recyclable: " + recyclable)
nextItem = next(items,None)
numAluminum = [0]
numPaper = [0]
numPlastic = [0]
while (nextItem != None) :
countItems(nextItem, numAluminum, numPaper, numPlastic)
nextItem = next(items,None)
"Number of aluminum items: "+str(numAluminum[0]) |> print
"Number of paper items: "+str(numPaper[0]) |> print
"Number of plastic items: "+str(numPlastic[0]) |> print