# # Compute the mode of a list. # # # Read the values from the user and store them in a list. # line = input("Enter an item to store in the list (blank to finish): ") values = [] while line != "": values.append(line) line = input("Enter an item to store in the list (blank to finish): ") # # Construct a dictionary where the keys are items from the list and the # values are the counts for how frequently each key occurred. # counts = {} for item in values: # If the item is one that we have seen previously if item in counts.keys(): counts[item] = counts[item] + 1 else: # Otherwise the item is one that we have not seen before counts[item] = 1 # # Compute the mode by finding the key (or keys) with largest value. # if len(counts) > 0: # Find the largest value in the dictionary largest = max(counts.values()) # Display all of the keys that have a value that is equal to the largest value. print("The mode or modes for the list is/are:") for k in counts.keys(): if counts[k] == largest: print(k) else: print("No values were entered so there is no mode to report.")