Check if this basic Python script is coded right
Terry Reedy
tjreedy at udel.edu
Sat Oct 26 15:46:43 EDT 2013
On 10/26/2013 2:20 PM, 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=?
>>
>> My script:
>> count = 0
>> answer = 0
>>
>> while count<200:
>> if count%3==0:
>> answer = answer + count**3
>> count = count + 1
>>
>> print ("Result is: " +str(answer))
>>
>> 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.
'Between 0-200' could well be interpreted to exclude both 0 and 200 and
to mean 'in 1-190'. Problem posers should be clear about ranges and
students should be careful to understand the problem given.
> 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.)
Unfortunately common.
--
Terry Jan Reedy
More information about the Python-list
mailing list