上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Interactive Loops One good use of the indefinite loop is to write interactive loops.Interactive loops allow a user to repeat certain portions of a program on demand. Remember how we said we needed a way for the computer to keep track of how many numbers had been entered?Let's use another accumulator,called count. 16
16 Interactive Loops • One good use of the indefinite loop is to write interactive loops. Interactive loops allow a user to repeat certain portions of a program on demand. • Remember how we said we needed a way for the computer to keep track of how many numbers had been entered? Let‟s use another accumulator, called count
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Interactive Loops At each iteration of the loop,ask the user if there is more data to process.We need to preset it to "yes"to go through the loop the first time. ·set moredata to "yes" while moredata is wyes" get the next data item process the item ask user if there is moredata 17
17 Interactive Loops • At each iteration of the loop, ask the user if there is more data to process. We need to preset it to “yes” to go through the loop the first time. • set moredata to “yes” while moredata is “yes” get the next data item process the item ask user if there is moredata
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Interactive Loops Combining the interactive loop pattern with accumulators for sum and count: ·initialize sum to0.0 initialize count to 0 set moredata to yes" while moredata is wyes" input a number,x add x to sum add 1 to count ask user if there is moredata output sum/count 18
18 Interactive Loops • Combining the interactive loop pattern with accumulators for sum and count: • initialize sum to 0.0 initialize count to 0 set moredata to “yes” while moredata is “yes” input a number, x add x to sum add 1 to count ask user if there is moredata output sum/count
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Interactive Loops average2.py 井 A program to average a set of numbers 井 Illustrates interactive loop with two accumulators def main(): moredata ="yes" sum =0.0 count 0 while moredata[0]=='y': x input ("Enter a number >>" sum sum x count count 1 moredata raw input ("Do you have more numbers (yes or no)?" print ("\nThe average of the numbers is",sum count) Using string indexing (moredata[o])allows us to accept y","yes”,yeah”to continue the loop 19
19 Interactive Loops # average2.py # A program to average a set of numbers # Illustrates interactive loop with two accumulators def main(): moredata = "yes" sum = 0.0 count = 0 while moredata[0] == 'y': x = input("Enter a number >> ") sum = sum + x count = count + 1 moredata = raw_input("Do you have more numbers (yes or no)? ") print("\nThe average of the numbers is", sum / count) • Using string indexing (moredata[0]) allows us to accept “y”, “yes”, “yeah” to continue the loop
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Interactive Loops Enter a number >32 Do you have more numbers (yes or no)?y Enter a number >45 Do you have more numbers (yes or no)?yes Enter a number >34 Do you have more numbers (yes or no)?yup Enter a number >76 Do you have more numbers (yes or no)?y Enter a number >45 Do you have more numbers (yes or no)?nah The average of the numbers is 46.4 20
20 Interactive Loops Enter a number >> 32 Do you have more numbers (yes or no)? y Enter a number >> 45 Do you have more numbers (yes or no)? yes Enter a number >> 34 Do you have more numbers (yes or no)? yup Enter a number >> 76 Do you have more numbers (yes or no)? y Enter a number >> 45 Do you have more numbers (yes or no)? nah The average of the numbers is 46.4