# # Compute the median of a collection of values entered by the user. The # user will use a blank line to indicate that no further values should be # entered and the median should be displayed. # # # Read the values from the user and store them in a list. # # Create a new empty list. values = [] # Read the first value from the user line = input("Enter a number (blank line to quit): ") # While the value entered by the user is not a blank line. while line != "": # Append the value to the end of the list as a float. values.append(float(line)) # Read the next value from the user. line = input("Enter a number (blank line to quit): ") # # Sort the list. # values.sort() # # Compute and display the median. # if len(values) == 0: print("No values were entered, so the median cannot be reported.") # If the list has odd length, report the middle element as the median. elif len(values) % 2 == 1: median = values[len(values) // 2] print(f"The median of those values is {median}.") # If the list has (non-zero) even length, report the average of the two # middle elements as the median. else: median = (values[len(values) // 2] + values[len(values) // 2 - 1]) / 2 print(f"The median of those values is {median}.")