943,986 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2004
  • Python RSS
Nov 5th, 2009
0

while loop in python

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laxter17 is offline Offline
3 posts
since Nov 2009
Nov 5th, 2009
0
Re: while loop in python
Could you be more clear on the conditions you want met?

Python Syntax (Toggle Plain Text)
  1. while True:
  2. answer = n * (n + 1) / 2
  3. print answer
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 5th, 2009
0
Re: while loop in python
Could you be more clear on the conditions you want met?

Python Syntax (Toggle Plain Text)
  1. while True:
  2. answer = n * (n + 1) / 2
  3. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laxter17 is offline Offline
3 posts
since Nov 2009
Nov 5th, 2009
0
Re: while loop in python
well I think you answered the question you know how to create the while loop here's a hint:

Python Syntax (Toggle Plain Text)
  1. >>> count = 0
  2. >>> while count < 5:
  3. print count
  4. count = count + 1
  5.  
  6.  
  7. 0
  8. 1
  9. 2
  10. 3
  11. 4
  12. >>>

Hope this helps
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 5th, 2009
0
Re: while loop in python
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.
Python Syntax (Toggle Plain Text)
  1. def sum3(n):
  2. i = 0
  3. while i <= n+1:
  4. sum = n * (n + 1) / 2
  5. print sum
  6. i += 1
  7. # same as i = i + 1
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 5th, 2009
0
Re: while loop in python
Click to Expand / Collapse  Quote originally posted by laxter17 ...
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.

Python Syntax (Toggle Plain Text)
  1. some_flag = initial_state
  2. while some_flag (does not meet) my_condition:
  3. perform action
  4. 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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 5th, 2009
0
Re: while loop in python
Thanks everybody for your help, I figured it out and got it to do what I wanted.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laxter17 is offline Offline
3 posts
since Nov 2009
Nov 5th, 2009
0
Re: while loop in python
Congratulations!
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Converting a dictionary into a list of (key, value) tuples
Next Thread in Python Forum Timeline: decimal help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC