# # Compute the mode (or modes) of a collection of values entered by the user. # # # Read all of the values from the user and store them in a list. # data = [] item = input("Enter an item (blank line to quit): ") while item != "": data.append(item) item = input("Enter an item (blank line to quit): ") # Create a new, empty dictionary. counts = {} # For each element in the list for item in data: # If the element is already a key in the dictionary if item in counts.keys(): # Increase the value associated with that key by 1 counts[item] = counts[item] + 1 # Otherwise else: # Add the key to the dictionary with a count of 1 counts[item] = 1 print(counts) # # The mode of the list is the key (or keys) with largest value # # Find the largest value in the dictionary largest = max(counts.values()) print("The mode (or modes) of the list is/are: ") # Display all of the keys that have largest value for key in counts.keys(): if counts[key] == largest: print(key)