Page 347 - Computer Science Class 11 With Functions
P. 347

For this purpose, we write the following functions:
              ●  readResult(): The function readResult() performs the following tasks:

               ○ Accepts from the user the number of students.
               ○ Accept from the user result of each student in the form of a list [rollNo, marks].
               ○ Constructs the list result comprising the list  [rollNo, marks] for each student.
              ●  marksUpdate(rollNo, marks): The function marksUpdate performs the following task:

               ○  To update the marks of a student, given his/her roll number and the updated marks. Given the roll number of
                   a student, the function looks for the list entry [rollNo, marks] within the list result and updates the
                   marks when the match is found.

              ●  allUpdates(): The function performs all necessary updates of the results.
               ○  The user is asked whether more updates to the result are required. He/she is prompted to enter his/her choice
                   by entering y/y or N/n. If the user opts for more updates, he/she is prompted to enter roll number and revised
                   marks of a student, and the function marksUpdate is invoked to update the marks of a student. The process
                   is repeated untilthe user says, no more updates are required.

            Having developed the functions  marksUpdate(),  readResult(),  and allUpdates(), the program
            merely needs to intialise the result list as an empty list and invoke these functions. (see Program 13.3)
              01 def marksUpdate(rollNo, marks):
              02     '''
              03     objective: To update result of a student
              04     Global data:
              05         result: list of lists of result of all students
              06     input parameters:
              07         rollNo: roll number of student for updating the result
              08         marks: updated marks
              09     Return value: None
              10     Side effect: List classResult gets updated
              11     '''
              12     for indx in range(len(classResult)):
              13         #look for rollNo in each subslist
              14         if(classResult[indx][0] == rollNo):
              15             classResult[indx][1] = marks
              16             break
              17
              18 #Input student data
              19 def readResult():
              20     '''
              21     objective: To read result of all students
              22     Global data:
              23         result: list of lists of result of all students
              24     Return value: None
              25     Side effect: List classResult gets updated
              26     '''
              27     nStudents = int(input('Enter number of students: '))
              28     print('Enter the list [Roll No,  Marks] and press enter: ')
              29     for i in range(0, nStudents):
              30         #Enter student result as a list: [rollNo, marks]
              31         result = eval(input('[rollNo, marks]: '))
              32         classResult.append(result)
              33
              34 def allUpdates():
              35     '''




                                                                                                Lists and Tuples  345
   342   343   344   345   346   347   348   349   350   351   352