Check if this basic Python script is coded right

Tim Delaney timothy.c.delaney at gmail.com
Sun Oct 27 17:30:57 EDT 2013


On 27 October 2013 23:20, rusi <rustompmody at gmail.com> wrote:

> On Saturday, October 26, 2013 11:50:33 PM UTC+5:30, MRAB wrote:
> > On 26/10/2013 18:36, HC wrote:
> > > I'm doing my first year in university and I need help with this basic
> assignment.
> > >
> > > Assignment: Write Python script that prints sum of cubes of numbers
> between 0-200 that are multiples of 3. 3^3+6^3+9^3+12^3....+198^3=?
> <code snipped>
> > >
> > > Is it all okay?
> > >
> > Just one small point: the assignment says that the numbers should be in
> > the range 0-200, but the loop's condition is count<200 it's excluding
> > 200, so the numbers will actually be in the range 0-199.
> >
> > However, as 200 isn't a multiple of 3, it won't affect the result, but
> > in another assignment it might.
> >
> > (For the record, this is known as an "off-by-one" error.)
>
> So is an off-by-one error that did not happen an off-by-an-off-by-one
> error?
>

I would say yes. When someone realises that the requirements were also off
by one and the specification gets changed to  "between 0-201" (inclusive)
then whoever fixes it might naively just add one to the existing code,
giving an incorrect result.

Obviously I'm ignoring the possibility of appropriate unit tests to prevent
this - just looking at the code itself.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131028/cbb4be8a/attachment.html>


More information about the Python-list mailing list