Solution to Exercise 3 (Day 1, morning session)

One of the entries in not_working.data (number 8 in the second line) contains an additional special character, and Python fails to convert it to an integer using the int function (line 31 in convert_to_dict.py). Python correctly raises an exception, but it is captured in the try … except statement to get_number_dict.

This kind of bug is quite common, and is due to forgetting to handle incorrect input in a function. There are two possible approaches to solving the bug: The first is to detect when the input cannot be converted to integer (e.g., by catching a ValueError around line 31); The second is to raise an appropriate exception. In this second case you should make sure to test that the walk_string function raises the correct exception, but also that get_number_dict does not stop it from being raised.

 
day1_ex3_solution.txt · Last modified: 2009/09/01 14:46 by pietro
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki