Tim Cooke

Exercises for Programmers (Python): 5 of 57 - Simple Math

23rd August 2020

Exercise 5 in book Exercises for Programmers: 57 Challenges to Develop Your Coding Skills by Brian P. Hogan.

Write a program that prompts for two numbers. Print the sum, difference, product, and quotient of those numbers as shown in the example output.

Example Output

What is the first number? 10
What is the second number? 5
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2

I'm getting some momentum now with these exercises so I'm going to dive right in with a set of tests since I've already decided that I need 4 functions to provide the math operations.

import unittest
from exercises.Ex05_simple_math import simplemath


class TestSimpleMath(unittest.TestCase):

    def test_givenValidNumbers_returnAdditionOfNumbers(self):
        self.assertEqual(simplemath.addition(10, 5), 15)

    def test_givenValidNumbers_returnDifferenceOfNumbers(self):
        self.assertEqual(simplemath.difference(10, 5), 5)

    def test_givenValidNumbers_returnProductOfNumbers(self):
        self.assertEqual(simplemath.product(10, 5), 50)

    def test_givenValidNumbers_returnQuotientOfNumbers(self):
        self.assertEqual(simplemath.quotient(10, 5), 2)

Moving right along with the required functions.

def addition(a, b):
    return a + b;


def difference(a, b):
    return a - b;


def product(a, b):
    return a * b;
   
    
def quotient(a, b):
    return a / b;

So far so good, but then I got to the script part to test it with user input and with a little exploratory testing it soon unravelled. An attempt to divide by zero causes havok.

if __name__ == "__main__":
    firstnum = input("What is the first number: ")
    secondnum = input("What is the second number: ")
    print(firstnum + " + " + secondnum + " = " + str(addition(int(firstnum), int(secondnum))))
    print(firstnum + " - " + secondnum + " = " + str(difference(int(firstnum), int(secondnum))))
    print(firstnum + " * " + secondnum + " = " + str(product(int(firstnum), int(secondnum))))
    print(firstnum + " / " + secondnum + " = " + str(quotient(int(firstnum), int(secondnum))))
$ python3 exercises/Ex05_simple_math/simplemath.py 
What is the first number: 10
What is the second number: 0
10 + 0 = 10
10 - 0 = 10
10 * 0 = 0
Traceback (most recent call last):
  File "exercises/Ex05_simple_math/simplemath.py", line 23, in <module>
    print(firstnum + " / " + secondnum + " = " + str(quotient(int(firstnum), int(secondnum))))
  File "exercises/Ex05_simple_math/simplemath.py", line 14, in quotient
    return a / b;
ZeroDivisionError: division by zero

A runtime error as expected, but at this point I'm not really clear about the specifics of how Python handles and reports errors and exceptions, so I'm expecting that to become clearer the further I get with these exercises. Now it's time to reproduce it with a unit test, but this raises the question of what to do with a divide by zero operation? The correct answer is infinity but can I represent that in Python? It turns out there are a few ways to do it, with float('inf'), and using the math library with math.inf. Using float('inf') does not require any further imports so I'm going with that.

def test_givenZeroDenominator_returnFloatInfinity(self):
    self.assertEqual(simplemath.quotient(10, 0), float('inf'))
def quotient(a, b):
    if (b == 0):
        return float('inf')
    return a / b

That's that sorted.

However, after some more exploratory testing I discover that I do not handle floating point numbers as input at all, and that's because I'm using int() to turn the input strings into integers. Perhaps it would be better to turn them all into floating point numbers with float() instead.

print(firstnum + " + " + secondnum + " = " + str(addition(float(firstnum), float(secondnum))))

Learning review

Github

57-exercises-python/src/exercises/Ex05_simple_math