I am totally new to programming and could use some help.

I am trying to write a while loop in python that could execute this equation but in a while loop type function.

the equation is n * (n + 1) / 2

any ideas?

Could you be more clear on the conditions you want met?

while True:
    answer = n * (n + 1) / 2
    print answer

Could you be more clear on the conditions you want met?

while True:
    answer = n * (n + 1) / 2
    print answer

I have written 2 functions already, they are:

def sum1(n):
result = n * (n + 1) / 2
print result

def sum2(n):
sum = 0
for x in range(1,n+1):
sum = x + sum
print sum

And this what I need to do for the third function:

The third function (call it "sum3") computes and prints the answer using a "while" loop. In some respects the code is similar to the "for" loop of part 2 above. However, unlike a "for" loop, you have to initialize a counting variable, test to see if it is small enough (using the while loop) and if so, execute the loop body. In the body you must increment the counting variable. This function is about 6 lines long.

well I think you answered the question :) you know how to create the while loop :) here's a hint:

>>> count = 0
>>> while count < 5:
	print count
	count = count + 1

	
0
1
2
3
4
>>>

Hope this helps :)

Im not really sure what your computing or what you need in the body but the while loop and the increment should be what your looking for.

def sum3(n):
    i = 0
    while i <= n+1:
        sum = n * (n + 1) / 2  
        print sum
        i += 1
        # same as i = i + 1

And this what I need to do for the third function:

The third function (call it "sum3") computes and prints the answer using a "while" loop. In some respects the code is similar to the "for" loop of part 2 above. However, unlike a "for" loop, you have to initialize a counting variable, test to see if it is small enough (using the while loop) and if so, execute the loop body. In the body you must increment the counting variable. This function is about 6 lines long.

We can't really do your homework for you, but the basics of a while loop are like this.

some_flag = initial_state
while some_flag (does not meet) my_condition:
    perform action
    update some_flag

So initially some_flag does not meet the desired condition. The loop continually updates the value of some_flag , and when it finally meets the condition that you specify, the loop automatically breaks.

You could alternately simply do a while True: loop (super loop or infinite loop), and then compare the flag to a condition using an if statement. If the flag meets the condition, use break to break out of the super loop.

Thanks everybody for your help, I figured it out and got it to do what I wanted.

This question has already been answered. Start a new discussion instead.