Check if this basic Python script is coded right

H

HC

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
 
J

Joel Goldstick

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

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

Mark Lawrence

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

John Ladasky

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 wantto 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 specificquestion.

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?
 
M

MRAB

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

Terry Reedy

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

Terry Reedy

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

rusi

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?
 
T

Tim Delaney

between 0-200 that are multiples of 3. 3^3+6^3+9^3+12^3....+198^3=?


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top