# # Compute the median of a collection of values that are all entered by the # user. # # # Read all of the values from the user and store them in a list. # # Create a new, empty list. data = [] # Read the first value from the user line = input("Enter a number (blank line to quit): ") # While the line entered by the user is not blank... while line != "": # Convert the user input into a number num = float(line) # Append the number to the list data.append(num) # Read the next value from the user line = input("Enter a number (blank line to quit): ") print(data) # # Sort the list of values. # data.sort() print(data) if len(data) == 0: print("A median cannot be computed because no values were entered.") else: # # Report the median as either middle value or the average of the two # "middle" values. # # If the length of the list is odd if len(data) % 2 == 1: # Median is the middle element in the list median = data[len(data) // 2] else: # The length of the list is even, so the median is the average of the two # "middle" elements. median = (data[len(data) // 2] + data[len(data) // 2 - 1]) / 2 print(f"The median of those values is {median}.")