Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment you are going to create a calculator. Upload a python script

ID: 3793248 • Letter: F

Question

For this assignment you are going to create a calculator. Upload a python script, named exactly calculator.py, to Blackboard.

There needs to be functions named exactly these:

Four arithmetic functions

add (x, y) subtract (x, y) divide (x, y) multiply (x, y)

These functions should perform the corresponding arithmetic on x and y. Return the result.

Four testing functions

test_add (x_input, y_input, expected_result) test_subtract (x_input, y_input, expected_result) test_divide (x_input, y_input, expected_result) test_multiply (x_input, y_input, expected_result)

These functions should take three inputs. The x_input, and y_input will be passed to the arithmetic function that is being tested. The testing functions should then compare the result of the arithmetic function to the expected result using an if statement. The testing functions should return True or False depending on whether the test passed or failed.

Your script should not run any of the functions.

Explanation / Answer

%This script is testing for multiple numbers.

%You can change accordingly for two numbers

%Run python calculator.py

from __future__ import division
import unittest

class Calculator(object):
"""Perform basic arithmetic"""
  
def __init__(self):
pass
  
def add(self, *nums):
"Adding all numbers in *nums sequentially"
total = 0
for i in nums:
total += i
return total


def subtract(self, *nums):
"Subtracting all numbers in *nums sequentially"
total = 0
for i in nums:
total -= i
return total
  
def multiply(self, *nums):
"Multiplying all numbers in *nums sequentially"
if len(nums) == 0:
return 0
total = 1
for i in nums:
total *= i
return total
  
  
def divide(self, *nums):
"Dividing 1 by all numbers in *nums sequentially"
if len(nums) == 0:
return 0
total = 1
for i in nums:
total /= i
return total

class CalculatorTest(unittest.TestCase):
  
  
def setUp(self):
"""Set up our test"""
self.calc = Calculator()
  
def test_add(self):
self.assertEqual(self.calc.add(9), 9)
self.assertEqual(self.calc.add(972, 13, 1312), 2297)
self.assertEqual(self.calc.add(100, 2, 99, 1), 202)
self.assertEqual(self.calc.add(11, 87, 29, 18, 19, 242, 7), 413)
  
def test_subtract(self):
self.assertEqual(self.calc.subtract(9), -9)
self.assertEqual(self.calc.subtract(972, 72, 800), -1844)
self.assertEqual(self.calc.subtract(100, 0, 99, 1), -200)
  
  
def test_multiply(self):
self.assertEqual(self.calc.multiply(9), 9)
self.assertEqual(self.calc.multiply(972, 13, 1312), 16578432)
self.assertEqual(self.calc.multiply(100, 2, 99, 1), 19800)
self.assertEqual(self.calc.multiply(11, 87, 29, 18, 19, 242, 7), 16078645044)
  
def test_divide(self):
self.assertEqual(self.calc.divide(9), 0.1111111111111111)
self.assertEqual(self.calc.divide(972, 13, 1312), 6.031933538708607e-08)
self.assertEqual(self.calc.divide(100, 2, 99, 1), 5.0505050505050505e-05)
self.assertEqual(self.calc.divide(11, 87, 29, 18, 19, 242, 7), 6.2194295431204e-11)


if __name__ == '__main__':
unittest.main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote