Jun-09-2022, 08:16 PM
Personally, I prefer to get the computer to do the work of checking the results. So, I write tests:
import unittest class SubtractElementsTest(unittest.TestCase): def test_it_subtracts_all_the_elements_from_the_list(self): result = subtractElements([100, 20, 4, 10]) self.assertEqual(result, 66) if __name__ == '__main__': unittest.main()Saving this in a file called
test_subtract_elements.py
and running it givesOutput:$ python3 test_subtract_elements.py
E
======================================================================
ERROR: test_it_subtracts_all_the_elements_from_the_list (__main__.SubtractElementsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_subtract_elements.py", line 6, in test_it_subtracts_all_the_elements_from_the_list
result = subtractElements([100, 20, 4, 10])
NameError: name 'subtractElements' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
So now I can gradually make it work, using the tests for feedback.