Page 144 - Information_Practice_Fliipbook_Class11
P. 144

•   A suite (lines 12-15), comprising the following two statements:

              marks = int(input('Enter marks:')) #S1
              sumMarks = sumMarks + marks   #S2
        Invoking the function range() with the argument nStudents yields a sequence of numbers 0, 1, 2,...
        nStudents-1 (i.e., 0, 1, 2,.., 5) because we have entered 6 as the value of the variable nStudents. Thus, the
        for statement serves as a means to count six numbers from 0 up to 5. Indeed, the variable count takes values in the
        range(0, nStudents) one by one. For each value of the variable count, each statement in the suite (following
        the header) is executed.

          1.  Initially, the count is 0. So, for count = 0, lines 12-15 are executed in sequence.
          2.   Since lines 12 and 13 are just comments, the Python interpreter doesn't care about them. So, only lines 14 and 15
            are executed by the Python interpreter.
          3.  On execution of line 14, the program reads a student's marks.
          4.  On execution of line 15, the marks that have just been read get added to sumMarks (initialised as 0).
          5.   Next, the Python interpreter checks whether the end of the sequence has already been reached. If so, the execution
            of the for statement comes to an end and the control moves to the statement following the for statement, or
            comes to an end if there are no statements following the for statement.
          6.   As the current value of variable count is 0, the next element in the sequence (let us denote it by next(count))
            is 1. So, for the updated value of count (=1), lines 14 and 15 are again executed in sequence, and marks
            obtained by the next student are read and added to sumMarks.
          7.  Next, the Python interpreter checks whether the end of the sequence has already been reached.
          8.   Continuing in this manner, when count reaches 5, lines 14 and 15 are again executed in order, and the next
            student's marks are read and added to sumMarks.
          9.  Next, the Python interpreter checks one more time whether the end of the sequence has already been reached.
         10.   Indeed, there are no more elements in the range(0, nStudents). So, the execution of the for statement
            comes to an end, and the control moves to line 16. The execution of line 16 computes the average marks.
         11.  Finally, the average marks are displayed on execution of line 17, and the program comes to an end.

         Program 6.2 Efficient computation of average marks of a group of students in an examination

          01 '''
          02 objective: To find the average marks of a group of students
          03 in an examination.
          04 Inputs: To be entered interactively
          05     nStudents: Number of studentsAccept data interactively
          06     marks: marks of students to be entered one ny one
          07 Output: Average marks
          08 '''
          09 sumMarks = 0
          10 nStudents = int(input('Enter number of students: '))
          11 for count in range(0, nStudents):
          12     #range(nStudents) includes 0, 1, ..., nStudents-1
          13     #Read and add to sumMarks a student's marks
          14     marks = int(input('Enter marks: ')) #S1
          15     sumMarks = sumMarks + marks  #S2
          16 averageMarks = sumMarks/nStudents
          17 print('average marks:', averageMarks)
        Sample Output:
         >>> Enter number of students: 6
         >>> Enter marks: 60
         >>> Enter marks: 70

          130  Touchpad Informatics Practices-XI
   139   140   141   142   143   144   145   146   147   148   149