In: Computer Science
Write a Python program that computes certain values such as sum, product, max, min and average of any 5 given numbers along with the following requirements.
Define a function that takes 5 numbers, calculates and returns the sum of the numbers.
Define a function that takes 5 numbers, calculates and returns the product of the numbers.
Define a function that takes 5 numbers, calculates and returns the average of the numbers. Must use the function you defined earlier to find the sum of the five numbers.
Define a function that takes 5 numbers, finds and returns the largest value among the numbers. Must use conditional statements and NOT use built-in max function.
Define a function that takes 5 numbers, finds and returns the smallest value among the numbers. Must use conditional statements and NOT use any built-in min function.
Define a function called main inside which Prompt users to enter 5 numbers. Call all the functions passing those 5 numbers entered by the user and display all the returned answers with proper descriptions.
Define a function called test For each of the functions you defined (6-10) write at least 2 automated test cases using assert statements to automatically test and verify that the functions are correctly implemented. make your program continue to run and calculate the same statistical values of as many sets of 5 numbers as a user wishes until they want to quit the program.
# calculator.py
from math import prod def my_sum(nums): return sum(nums) def my_product(nums): return prod(nums) def my_average(nums): return my_sum(nums)/len(nums) def largest(nums): m = nums[0] for i in range(len(nums)): if nums[i] > m: m = nums[i] return m def smallest(nums): s = nums[0] for i in range(len(nums)): if nums[i] < s: s = nums[i] return s if __name__ == "__main__": while True: nums_input = list(map(int, input("Please enter 5 space separated numbers (enter 0 to exit): ").split())) if nums_input == [0]: break print(f"Sum of entered numbers = {my_sum(nums_input)}") print(f"Product of entered numbers = {my_product(nums_input)}") print(f"Average of entered numbers: {my_average(nums_input)}") print(f"Largest number among entered numbers = {largest(nums_input)}") print(f"Smallest number among entered numbers = {smallest(nums_input)} \n")
# test.py
import unittest import calculator as c class Test(unittest.TestCase): def test_calculator1(self): test_parameter = [1, 2, 3, 4, 5] result = [c.my_sum(test_parameter), c.my_product(test_parameter), c.my_average(test_parameter), c.largest(test_parameter), c.smallest(test_parameter)] self.assertEqual(result, [15, 120, 3.0, 5, 1]) def test_calculator2(self): test_parameter = [0, 15, 10, 5, 20] result = [c.my_sum(test_parameter), c.my_product(test_parameter), c.my_average(test_parameter), c.largest(test_parameter), c.smallest(test_parameter)] self.assertEqual(result, [50, 0, 10.0, 20, 0]) unittest.main()