while loop in python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: laxter17 is an unknown quantity at this point 
Solved Threads: 0
laxter17 laxter17 is offline Offline
Newbie Poster

while loop in python

 
0
  #1
34 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 54
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 8
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #2
34 Days Ago
Could you be more clear on the conditions you want met?

  1. while True:
  2. answer = n * (n + 1) / 2
  3. print answer
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: laxter17 is an unknown quantity at this point 
Solved Threads: 0
laxter17 laxter17 is offline Offline
Newbie Poster
 
0
  #3
34 Days Ago
Originally Posted by ShadyTyrant View Post
Could you be more clear on the conditions you want met?

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 240
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 58
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #4
34 Days Ago
well I think you answered the question you know how to create the while loop here's a hint:

  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
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 54
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 8
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #5
34 Days Ago
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,057
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 266
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #6
34 Days Ago
Originally Posted by laxter17 View Post
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.

  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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: laxter17 is an unknown quantity at this point 
Solved Threads: 0
laxter17 laxter17 is offline Offline
Newbie Poster
 
0
  #7
34 Days Ago
Thanks everybody for your help, I figured it out and got it to do what I wanted.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 54
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 8
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #8
34 Days Ago
Congratulations!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC