General

Chat

Python Coding Help

Hey guys I have this comp sci homework in Python that I can't figure out...

"Write a Python function named sumOfEven(n) that uses a loop to calculate the sum of the first
n even natural numbers then displays the result (we will not consider 0 to be a natural number).
A couple of sample runs are shown below.

>>> sumOfEven(0)
The sum of the first 0 even numbers is 0
>>> sumOfEven(1)
The sum of the first 1 even numbers is 2
>>> sumOfEven(2)
The sum of the first 2 even numbers is 6
>>> sumOfEven(10)
The sum of the first 10 even numbers is 110"

I tried to say: print("The sum of the first",n,"even numbers is",(n * (n + 1)) but the teacher said that I have to use a "for i in range (n)" loop

I think it's supposed to be really simply but i can't figure it out >.<

October 5, 2013

6 Comments • Newest first

iceychill2

@BobR: oh, that works. but I actually already got it haha

if you're curious, i ended up writing:

def sumOfEven(n):
total = 0
for i in range(n):
nsum = 2 + (2 * i)
total = total + nsum
print("The sum of the first",n,"even numbers is",total)

this might actually be a different way than what you had mentioned.. but i guess there's more than one way to skin a cat

@wasp3: ehh. that could've been better.

Reply October 5, 2013 - edited
wasp3

Call a snake expert or animal control or your mom? What ever you do, don't let it coil around your neck.

Reply October 5, 2013 - edited
BobR

@iceychill2 The assignment is to use a loop to calculate the results, not a simple formula.

You need to set up a loop that will calculate the total by adding EACH even number starting with the first (2) and ending with the last (n).
It will go through each even number and add it to the total until it hits (n) and stops.

So... as the loop runs, when i = 2, the total = 2
when i = 4, the total = total + i (or 6)
when i = 6, the total = total + i (or 12)
and so on until you hit n where the total will = total + i for the final result.
Then you'd print "The sum of the first (i) even numbers is (total)".

This is not a great example of using loops, since the total can be calculated more easily like your original formula did, it's just intended to show how looping can be used to calculate a total by repeatedly adding a changing value to the total.

Reply October 5, 2013 - edited
nikeball123

Idk how python works, but in c++ you could use the for loop.

int x = (whatever you want/how many numbers do you want?);
bool y;
for (i=0 i>x i++)
{ if i%2=0
y= true
if true func();
}

Reply October 5, 2013 - edited
xoqtprincessxo

The general idea is to run through a loop of the size n in your function and keep adding to a sum variable each time you iterate.

Every time you loop, add 2n to your sum, and you can print out what sum is after you finish looping.

Reply October 5, 2013 - edited
Arios

Use the for loop your teacher suggested.
Create a variable for the sum of even numbers.
sum of even numbers += 2*i
return the sum of even numbers

Reply October 5, 2013 - edited