Check-in 3: Fix unit-tested code
Once you’ve written tests for the numberfmt module,
and used them to expose the three bugs,
Try fixing the code for formatInt so that all the tests pass.
- For the
size=0bug, it’s okay to requiresize>0as a precondition, and then test for theAssertionError. - For the leading-zero bug, the problem is that
str(0)produces"0"rather than"000", or"48"when it should be"048". Instead of usingstr(), you can use Python format strings like this:>>> "%0*d" % (8, 415) '00000415' >>> "%0*d" % (5, 415) '00415'
where the first number in the pair is the
size. - The trickiest is probably the negative number bug. I suggest
checking for a negative number explicitly, and either setting a
boolean flag, or else a string to represent the
sign.if num < 0: sign = "-" num = - num # Make it positive else: sign = ""
Then you can proceed with doing the formatting for the positive number, and when done, stick the
signout front.
Before committing, you should create a file at the top of your
repository called .gitignore (notice the leading dot), with the
following lines:
__pycache__/ .coverage htmlcov/
That will ensure that temporary object files and coverage reports do not get committed to the repository.