Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57638 > unrolled thread

Check if this basic Python script is coded right

Started byHC <hikmat@jafarli.net>
First post2013-10-26 10:36 -0700
Last post2013-10-26 16:16 -0400
Articles 9 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Check if this basic Python script is coded right HC <hikmat@jafarli.net> - 2013-10-26 10:36 -0700
    Re: Check if this basic Python script is coded right Joel Goldstick <joel.goldstick@gmail.com> - 2013-10-26 13:53 -0400
    Re: Check if this basic Python script is coded right Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-26 18:55 +0100
    Re: Check if this basic Python script is coded right John Ladasky <john_ladasky@sbcglobal.net> - 2013-10-26 10:58 -0700
    Re: Check if this basic Python script is coded right MRAB <python@mrabarnett.plus.com> - 2013-10-26 19:20 +0100
      Re: Check if this basic Python script is coded right rusi <rustompmody@gmail.com> - 2013-10-27 05:20 -0700
        Re: Check if this basic Python script is coded right Tim Delaney <timothy.c.delaney@gmail.com> - 2013-10-28 08:30 +1100
    Re: Check if this basic Python script is coded right Terry Reedy <tjreedy@udel.edu> - 2013-10-26 15:46 -0400
    Re: Check if this basic Python script is coded right Terry Reedy <tjreedy@udel.edu> - 2013-10-26 16:16 -0400

#57638 — Check if this basic Python script is coded right

FromHC <hikmat@jafarli.net>
Date2013-10-26 10:36 -0700
SubjectCheck if this basic Python script is coded right
Message-ID<9716695c-31e3-40a0-b2a4-73b967494107@googlegroups.com>
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?

Regards

[toc] | [next] | [standalone]


#57639

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-10-26 13:53 -0400
Message-ID<mailman.1592.1382810012.18130.python-list@python.org>
In reply to#57638
On Sat, Oct 26, 2013 at 1:36 PM, HC <hikmat@jafarli.net> 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?
>
> Regards
> --
> https://mail.python.org/mailman/listinfo/python-list

You should run it to see what results you get.  I am guessing that you
don't know the ultimate answer, but you could run it for a much
smaller number than 200 and check the results by hand.



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#57640

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-26 18:55 +0100
Message-ID<mailman.1593.1382810134.18130.python-list@python.org>
In reply to#57638
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?
>
> Regards
>

Simplify it by using a for loop with range(count) and you can also write 
answer += count**3.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#57641

FromJohn Ladasky <john_ladasky@sbcglobal.net>
Date2013-10-26 10:58 -0700
Message-ID<e5d58dce-110f-4614-9587-0731185ae2da@googlegroups.com>
In reply to#57638
HC, you have come straight out and told us honestly that you are seeking help with homework.  That is refreshing.  

You should know that people in programming newsgroups generally do not want to do your homework for you.  Many of us are willing to teach you HOW to program, but that's different than just handing you the answer to a specific question.

Do you know how to CHECK whether the output of your program is correct yourself?  Did you run it?  What output did you get?  Did it tell you anything useful?  If it did not tell you anything useful, what simple changes might you make to tell you something useful?

[toc] | [prev] | [next] | [standalone]


#57642

FromMRAB <python@mrabarnett.plus.com>
Date2013-10-26 19:20 +0100
Message-ID<mailman.1594.1382811634.18130.python-list@python.org>
In reply to#57638
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.

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.)

[toc] | [prev] | [next] | [standalone]


#57739

Fromrusi <rustompmody@gmail.com>
Date2013-10-27 05:20 -0700
Message-ID<d255467a-c965-4fbb-960e-ff532618fdef@googlegroups.com>
In reply to#57642
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?

[toc] | [prev] | [next] | [standalone]


#57766

FromTim Delaney <timothy.c.delaney@gmail.com>
Date2013-10-28 08:30 +1100
Message-ID<mailman.1664.1382909888.18130.python-list@python.org>
In reply to#57739

[Multipart message — attachments visible in raw view] — view raw

On 27 October 2013 23:20, rusi <rustompmody@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

[toc] | [prev] | [next] | [standalone]


#57654

FromTerry Reedy <tjreedy@udel.edu>
Date2013-10-26 15:46 -0400
Message-ID<mailman.1602.1382816824.18130.python-list@python.org>
In reply to#57638
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

[toc] | [prev] | [next] | [standalone]


#57657

FromTerry Reedy <tjreedy@udel.edu>
Date2013-10-26 16:16 -0400
Message-ID<mailman.1605.1382818600.18130.python-list@python.org>
In reply to#57638
On 10/26/2013 1:36 PM, 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?

Generalize the problem, put the code in a reusable testable function, 
and start with test cases.
--
def sum33(n):
   "Return sum of cubes of multiples of 3 <= n."

for n, sm in ( (0,0), (2,0), (3,27), (4,27), (6,243), ):
   assert sum33(n) == sm

print(sum33(200))
--
This will fail because None != 0. Now add 'return 0' and the first two 
cases pass and then it fails with None != 27. Now change 'return 0' to

   answer = 0
   for i in range(3, n+1, 3):
     answer += i*i*i
   return answer

and it will print 131990067, which I presume is correct. Putting your 
code in the body instead, indented, with '200' replaced with 'n+1', and 
with 'return answer' added, gives the same result after passing the same 
tests.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web