| | |
while loop in python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
0
#2 34 Days Ago
Could you be more clear on the conditions you want met?
Python Syntax (Toggle Plain Text)
while True: answer = n * (n + 1) / 2 print answer
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
0
#3 34 Days Ago
•
•
•
•
Could you be more clear on the conditions you want met?
Python Syntax (Toggle Plain Text)
while True: answer = n * (n + 1) / 2 print answer
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.
0
#4 34 Days Ago
well I think you answered the question
you know how to create the while loop
here's a hint:
Hope this helps
you know how to create the while loop
here's a hint: Python Syntax (Toggle Plain Text)
>>> count = 0 >>> while count < 5: print count count = count + 1 0 1 2 3 4 >>>
Hope this helps
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.
Python Syntax (Toggle Plain Text)
def sum3(n): i = 0 while i <= n+1: sum = n * (n + 1) / 2 print sum i += 1 # same as i = i + 1
0
#6 34 Days Ago
•
•
•
•
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.
Python Syntax (Toggle Plain Text)
some_flag = initial_state while some_flag (does not meet) my_condition: perform action update some_flag
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. ![]() |
Similar Threads
- Web-Scrape Loop via CSV list of URLs ??? (Python)
- do-while loop in python (Python)
- While Loop (Python)
- python script help (Python)
- Problems teaching myself Python (Python)
- Endless Loops (Python)
Other Threads in the Python Forum
- Previous Thread: Converting a dictionary into a list of (key, value) tuples
- Next Thread: decimal help
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog cx-freeze data decimals dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui halp heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






