# Count the number of occurences of an item in a list using a for loop # Parameters: # data: The list containing the items to count # what: The item that we are looking for # Returns: The number of times that what occurs in data def count(data, what): total = 0 for item in data: if item == what: total = total + 1 return total # Count the number of occurences of an item in a list using a while loop that # modifies the list # Parameters: # data: The list containing the items to count # what: The item that we are looking for # Returns: The number of times that what occurs in data def count2(data, what): total = 0 while data != []: item = data.pop() if item == what: total = total + 1 return total print("Using the count function (for loop version):") values = ["a", "b", "c", "a", "a", "b"] print(" values is currently", values) print(" How many times does a appear in values?", count(values, "a")) print(" values is currently", values) print(" How many times does b appear in values?", count(values, "b")) print(" values is currently", values) print(" How many times does c appear in values?", count(values, "c")) print(" values is currently", values) print() print("Using the count2 function (while loop version):") values = ["a", "b", "c", "a", "a", "b"] print(" values is currently", values) print(" How many times does a appear in values?", count2(values, "a")) print(" values is currently", values) print(" How many times does b appear in values?", count2(values, "b")) print(" values is currently", values) print(" How many times does c appear in values?", count2(values, "c")) print(" values is currently", values)