Fun with Statistics Part II In this practice program, you will be modify the program you wrote for Practice 5 - Fun with Statistics, to append the user input to a list. When the user is done entering
# Sample Run:
# Enter a number (or 'q' to quit): 5
# Enter a number (or 'q' to quit): 10
# Enter a number (or 'q' to quit): 3
# Enter a number (or 'q' to quit): q
# Sum: 18
# Average: 6.0
# Minimum: 3
# Maximum: 10
def get_user_input():
"""
Get a number from the user.
Returns:
int: The number entered by the user.
"""
user_input = input("Enter a number (or 'q' to quit): ")
return user_input
def calculate_statistics(numbers):
"""
Calculate the sum, average, minimum, and maximum of a list of numbers.
Args:
numbers (list): A list of integers.
Returns:
tuple: A tuple containing the sum, average, minimum, and maximum of the numbers.
"""
total_sum = sum(numbers)
average = total_sum / len(numbers) if numbers else 0
minimum = min(numbers) if numbers else None
maximum = max(numbers) if numbers else None
return total_sum, average, minimum, maximum
def print_statistics(total_sum, average, minimum, maximum):
"""
Print the sum, average, minimum, and maximum of a list of numbers.
Args:
total_sum (int): The sum of the numbers.
average (float): The average of the numbers.
minimum (int): The minimum number.
maximum (int): The maximum number.
"""
print(f"Sum: {total_sum}")
print(f"Average: {average}")
print(f"Minimum: {minimum}")
print(f"Maximum: {maximum}")
def main():
"""
Main function to run the program.
"""
numbers = []
while True:
user_input = get_user_input()
if user_input.lower() == 'q':
break
try:
number = int(user_input)
numbers.append(number)
except ValueError:
print("Please enter a valid integer or 'q' to quit.")
total_sum, average, minimum, maximum = calculate_statistics(numbers)
print_statistics(total_sum, average, minimum, maximum)
if __name__ == "__main__":
main()