# # Compute the median of a collection of values entered by the user. # # # Read numbers from the user until a blank line is entered. # # Parameters: (None) # # Returns: A list of all the values entered by the user as floating point # numbers. # def readNumbers(): # Read the first value from the user line = input("Enter a number: ") # Create an empty list to store the values data = [] # While the user has not entered a blank line while line != "": # Convert the entered value to a floating point number num = float(line) # Add the number to the list of values data.append(num) # Read the next line from the user line = input("Enter a number: ") # Return the list of values read from the user return data # # Compute the median of a collection of values. # # Parameters: # data: A non-empty list of numbers. # # Returns: The median of the numbers stored in data. # def median(data): # # Sort the values. # data.sort() # # Compute the median. # if len(data) % 2 == 0: # An even number of values were entered so the median is the average # of the two middle values median = (data[len(data) // 2] + data[len(data) // 2 - 1]) / 2 else: # An odd number of values was entered so the median is the middle value median = data[len(data) // 2] # # Return the median. # return median # # Compute the median of a collection of values entered by the user # def main(): values = readNumbers() if len(values) == 0: # No values were entered print("The median cannot be computed because no values were entered.") else: # One or more values were entered so the median can be computed and # reported. m = median(values) print("The median value is", m) main()