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...
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 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.
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a FORTRAN program that computes the average of a collection of numbers and then outputs...
Write a FORTRAN program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C 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. 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
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg) Joe 1.82 72.57 Mary...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT