Question

In: Computer Science

Write a Python program that computes certain values such as sum, product, max, min and average...

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.

Solutions

Expert Solution

# 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()

Related Solutions

c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
Exercise 9.2 (c++) (max, min, average, and median code) Write a program that asks users to...
Exercise 9.2 (c++) (max, min, average, and median code) Write a program that asks users to input up to 20 integers, and then finds the maximum, minimum, average, and median of the numbers that were entered. Use the following information to write your program. The median is the number that appears in the middle of the sorted list of numbers. If the array has an odd number of elements, median is a single number in the middle of the list...
Consider the following code section of a parallel program that computes the sum of n values...
Consider the following code section of a parallel program that computes the sum of n values using p processors: my_sum = 0; my_first_i = ... ; my_last_i = ... ; for(my_i = my_first_i; my_i < my_last_i; my_i++) { my_x = Compute_next_value(...); my_sum += my_x; } Here the prefix my_ indicates that each core is using its own, private variables, and each core can execute this block of code independently of the other cores. Devise formulas for the functions that calculate...
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input.
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input. (Java language)
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a Python program that computes the income tax for an individual. The program should ask...
Write a Python program that computes the income tax for an individual. The program should ask the user to enter the total taxable income for the year. The program then uses the tax bracket (as shown below) to calculate the tax amount. This is based on a progressive income tax system which is how income tax is calculated in the U.S. As a result, this for example means that only income above $500,001 is taxed at 37%. Income of lower...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT