 # Help for a program

Hi,

in college we got an homework. The task is to write a program that sum the first consecutive even numbers and that the sum can not exceed 200.
The result would be:
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 + 22 + 24 + 26

The count of even numbers is: 13
The total is: 182

I am sure that this is an easy task for most of you, but I am a beginner so please be patient .

I am trying for a couple of days now, but I am not able to write the right code.

So far I was able to do this:

from random import *
from math import *

even = 1
value = 0

while even <= 200:
even = (even + 2)
value = value +1

``````print ("sum of the value: ", even , "number of the value: ", value )
if even == 200:
break;
``````

total = int(input("How many consecutive numbers should I add up not to exceed 200: "))

## if total == 5: print("You calculated correctly: ", value - 1, “consecutive numbers is the correct answer”)

Thank you for your help in college we got an homework. The task is to write a program that sum
the first consecutive even numbers and that the sum can not exceed 200.
The result would be:
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 + 22 + 24 + 26

The count of even numbers is: 13
The total is: 182

Do I take it that you need to sum the even numbers in their natural
order (2, 4, 6, … as in your example above), and not that you’re given
a mixed sequence of numbers where you need to recognise consection runs
of evens. I’ll assume the former.

I am sure that this is an easy task for most of you, but I am a
beginner so please be patient .
I am trying for a couple of days now, but I am not able to write the
right code.

So far I was able to do this:
[…]
even = 1
value = 0

while even <= 200:
even = (even + 2)
value = value +1

print ("sum of the value: ", even , "number of the value: ", value )
if even == 200:
break;

total = int(input("How many consecutive numbers should I add up not to exceed 200: "))

if total == 5:
print("You calculated correctly: ", value - 1, “consecutive numbers is the correct answer”)

This seems a bit odd. Would not 13 be the target, given your example?

The code above does not seem to do any addition, making computing a sum
difficult. Also, you start with even=1. That is not an even number!

I would:

``````start with even=0 and total=0
set the loop condition to "total <= 200"
``````

Inside the loop: bump “even” by 2 as you do. Compute a new_total = total

• even. If that exceeds 200, break out of the loop without updating
“total”. otherwise set total=new_total.

You would also want a counter of how many times you have run the loop,
in order to count the number of values you added up.

I’d get rid of the input() entirely: just run the programme, and print
the final results as in the example you gave at the top.

I would also put some print() calls inside the loop so that you can see
the values of "even’ and “total” as they go up, to check that the
correct things are happening; remove those when the behaviour is
correct.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Hi Natalija,

Before you can solve a problem by writing a program, it helps to be able
to solve the problem using pencil and paper. So let’s try that first.

We want to add up some even numbers, one at a time, giving a running
total. We stop when the running total is bigger than 200, and count how
many even numbers were added. Using pencil and paper, you might write it
like this:

``````    0  # The running total starts at zero.
+   2  # That's ONE even number.
gives
2
+   4  # The SECOND even number.
gives
6
+   6  # The THIRD even number.
gives
12
+   8  # The FOURTH even number.
``````

and so on.

So we need at least three variables:

• the running total, which starts at 0

• the even number to add, which starts at 2 and increases by 2 each time

• and the count of how many even numbers we added, which starts at 0
and increases by 1 each time.

So we can start by writing this:

``````running_total = 0
even_number = 2
count = 0
while running_total < 200:
running_total = running_total + even_number
count = count + 1
even_number = even_number + 2  # The number we use *next*

print("The count of even numbers is:", count)
print("The total is:", running_total)

``````

If you try running that code, you will see that it seems to work,
but it takes one step too many. Try it and see. What do you get?

Now think about how you can adjust the code so that it reports the
correct answer. Start by explaining what you would do using pencil and
paper.

Think about how you would explain it to a little kid, who knows
how to do addition and subtraction, but otherwise isn’t too bright. What
instructions could you give them to go from the count and total which
are too high, to the correct count and total?