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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
Offline 1,102 posts
since Jul 2008