Tests are failing for Python Numbers exercise

My apologies if this is not the proper outlet for this type of technical issue. It wasn’t clear if this should be done on Github or via the forum.

I’ve been trying to figure out the cause of this error I’m getting while running a test for a single function (exchange_money()). I’m struggling to track this down due to my unfamiliarity with python. I’ve tested my changes using a python REPL, and the result is exactly what I’d expect, but the test for this function fails.

Here’s my code:

def exchange_money(budget, exchange_rate):
    """

    :param budget: float - amount of money you are planning to exchange.
    :param exchange_rate: float - unit value of the foreign currency.
    :return: float - exchanged value of the foreign currency you can receive.
    """

    budget / exchange_rate

And the Test function:


    @pytest.mark.task(taskno=1)
    def test_exchange_money(self):
        input_data = [(100000, 0.8), (700000, 10.0)]
        output_data = [125000, 70000]

        for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
            with self.subTest(f"variation #{variant}", input_data=input_data, output_data=output_data):
                self.assertAlmostEqual(exchange_money(input_data[0], input_data[1]), output_data)

This is the output from running this test:

============================= test session starts ==============================
collecting ... collected 1 item

exchange_test.py::CurrencyExchangeTest::test_exchange_money 

======================== 1 failed, 6 warnings in 0.03s =========================
FAILED       [100%]
exchange_test.py:13 (CurrencyExchangeTest.test_exchange_money)
self = <exchange_test.CurrencyExchangeTest testMethod=test_exchange_money>

    @pytest.mark.task(taskno=1)
    def test_exchange_money(self):
        input_data = [(100000, 0.8), (700000, 10.0)]
        output_data = [125000, 70000]
    
        for variant, (input_data, output_data) in enumerate(zip(input_data, output_data), start=1):
            with self.subTest(f"variation #{variant}", input_data=input_data, output_data=output_data):
>               self.assertAlmostEqual(exchange_money(input_data[0], input_data[1]), output_data)
E               TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

exchange_test.py:21: TypeError

Process finished with exit code 1

Is my solution actually incorrect? What should I do to ensure this test passes?

1 Like

What value does your function return, i.e. what follows the return keyword in your function?

1 Like

Ah I see. I hadn’t realized an explicit return was required for the value to be returned to the caller. Prepending an explicit return resulted in the test passing. Thanks for your help!

The error complains that somewhere a value of type NoneType appeared in a place where an int was required. How did that happen?

The answer is that when execution reaches the end of the function body, then None is returned implicitly, as if the last statement were return None. The type of None is NoneType, hence the error.

Pylint might have been able to catch this mistake for you.

1 Like

Without testing it, I’m not sure that there is anything for pylint to flag in that code. There’s nothing wrong with implicit returns.

You’re right; I have had Pylint warn me that either all or none of the paths should return explicitly, but @ZorgonPeterson’s code very likely already had that property.