Python solution

profileart08
week8tutorialactivity.docx

( Week 8 Practical Activity )

In week 5 you worked on question 1 and 2 of this document and in week 6 you worked on questions 3 and 4. The week 6 solution is provided on pages 4-6. In week 7 you worked on questions 5 and 6. Solution to week 7 activity is provided on pages 6-7. The week 8 practical activity includes completing question 7 and 8.

The following diagram shows the control flow among the functions to solve your project. Each rounded rectangle represents a function and the text in the rectangle denotes the function name. The arrows indicate which function(s) can be invoked from a parent function. The control flow starts at level 1 with the menu function. The menu function does not have a parent function; rather it is invoked by the Python interpreter when you click the run button on PyCharm. The menu function in turn invokes enterAllMkarksOfAStudentToStorage and displayAllReports. Similarly the searchStudentMarks function gets invoked by enterAllMkarksOfAStudentToStorage. Similarly the order of invocation of all other function is shown. It is important to remember that the control flow diagram does not indicate the order of execution at a specific level. For example the figure does not indicate whether enterAllMkarksOfAStudentToStorage or displayAllReports get executed first at level 2 even though enterAllMkarksOfAStudentToStorage appears to the before displayAllReports at level 2

( Level 1 Level 2 Level 3 Level 3 Level 3 Level 3 Level 3 Level 4 )

( menu )

( displayAllReports )

( enterAllMkarksOfAStudentToStorage )

( getBelowAvgDFReportMarksReport ) ( searchStudentMarks )

( getBelowAvgProjectMarksReport )

( getBelowAvgFinalExamMarksReport )

( getBelowAvgOverallMarksReport )

( displaySelectedStudentsMarks )

( displayAStudentsDetail )

Using PyCharm create a python project called ISYS217ProjectWeek5Version. Note you will have a newer version of this code each week. So do not lose the solution

Question 1: Write the definition of a menu function such that when it executes it prompts the user to enter 1 or 2 or 3. Further if the user enters 1 it invokes the enterAllMkarksOfAStudentToStorage function and if the user enters 2 it invokes the displayAllReports function and if the user enter 3 it exits the program.

At this stage do not bother about the finer details of enterAllMkarksOfAStudentToStorage and displayAllReports. That is assume the ad-hoc function definition of enterAllMkarksOfAStudentToStorage is specified as follows:
def enterAllMkarksOfAStudentToStorage():
	print(“The enterAllMkarksOfAStudentToStorage function got invoked\n”)
Similarly assume the ad-hoc function definition of displayAllReports is specified as follows:
def displayAllReports ():
	print(“The displayAllReports function got invoked\n”)

Question 2:

Using similar reasoning re-write the displayAllReports function definition. That is when displayAllReports gets invoked it should prompt the user to enter:

1 for the invocation of getBelowAvgDFReportMarksReport

2 for the invocation of getBelowAvgProjectMarksReport

3 for the invocation of getBelowAvgFinalExamMarksReport

4 for the invocation of getBelowAvgOverallMarksReport

5 for the invocation of displaySelectedStudentsMarks

6 to return to the menu function

Note: As you have done for question 1 user similarly ad-hoc function definitions for each of the above mentioned function definitions.

Question 3:

Since you have learnt while loops in week 6, you will have to change the function definitions of the menu function such that the program exits only if the user enters 3. Remember in week 5 your program was exiting even if you did not enter 3. This can be done using a while loop.

Question 4:

Similarly you have to change the function definition of enterAllMkarksOfAStudentToStorage such that the user may enter valid marks for DF Report, Project and Final exam for a student. This would also mean entering the name of the student. You may recall that the valid mark for a DF Report is between 0 and 20. Similarly a valid mark for Project is between 0 and 30 and that for the final exam is between 0 and 50. Use the while loop to ensure that the program repeatedly asks the user to enter the valid marks if a wrong entry is made for any of the 3 assessments.

Observe from the figure given on page 1 that the enterAllMkarksOfAStudentToStorage function invokes the searchStudentMarks function to find if a new student’s name (or similar uniquely identifiable detail) already exists in the storage. We will not want to get distracted by this detail at the moment. Hence write a dummy function definition for searchStudentMarks as follows:

def searchStudentMarks (keyToSearch):
	print(“The searchStudentMarks function got invoked\n”)

The enterAllMkarksOfAStudentToStorage functions should add the students detail preferably as a tuple or as a list in a dictionary data-structure that is supplied as a parameter to the function. You may use the student name or student id as the key in the dictionary. Assume the tuple (or list) details are organized as follows:

First item contains df report marks

Second item contains project marks

Third item contains final exam marks

Finally enterAllMkarksOfAStudentToStorage function should return the dictionary object.

#-----------------------Week 6 Solution----------------------------------------------------

def menu():

allStudentMarks = {} #Storage for all students marks. The storage is in the form of a dictionary data structure

while True:

kbinput = input("Enter 1 to store student details\n" +

"Enter 2 to display student report\n" +

"Enter any other key to exit\n")

if kbinput == '1':

allStudentMarks = enterAllMarksOfAStudentToStorage(allStudentMarks)

elif kbinput == '2':

displayAllReports()

elif kbinput!='1' and kbinput != '2':

break #break could have been replaced by return. return returns the control flow out of the function

#break breaks the control out of the loop

def enterAllMarksOfAStudentToStorage(allStudentMarks):

fname = input("Enter a student's full name\n")

#invoke searchStudentMarks to ensure student name is unique; this will be done in week 7

#enter valid marks for DF Report; valid marks range: 0 to 20

while True:

dfReportMarks = input("enter marks for DF Report; valid marks range: 0 to 20\n")

dfReportMarks = float (dfReportMarks)

if dfReportMarks >= 0 and dfReportMarks <= 20:

break

#enter valid makrs for Project: valid marks range: 0 to 30

while True:

projectMarks = input("enter marks for Project; valid marks range: 0 to 30\n")

projectMarks = float (projectMarks)

if projectMarks >= 0 and projectMarks <= 30:

break

while True:

finalExamMarks = input("enter marks for Final Exam; valid marks range: 0 to 50\n")

finalExamMarks = float (finalExamMarks)

if finalExamMarks >= 0 and finalExamMarks <= 30:

break

studentMarks = [dfReportMarks, projectMarks, finalExamMarks]

allStudentMarks[fname] = studentMarks

##temporary code to display current dictionary content

for k, v in allStudentMarks.items():

print ("Student ", k, "'s marks are \n")

for m in v:

print(m, "\t")

return allStudentMarks

def displayAllReports():

kbinput = input("Enter 1 to get DF Report Marks\n" +

"Enter 2 to get Project Marks\n" +

"Enter 3 to get Final Exam Marks\n" +

"Enter 4 to get Overall Marks\n" +

"Enter 5 to get Selected Student Marks\n" +

"Enter 6 to get to Menu\n")

if kbinput == 1:

getBelowAvgDfReportMarksReport()

if kbinput == 2:

getBelowAvgProjectMarksReport()

if kbinput == 3:

getBelowAvgFinalExamMarksReport()

if kbinput == 4:

getBelowAvgOverallMarksReport()

if kbinput == 5:

displaySelectedStudentsMarks()

if kbinput == 6:

return

def getBelowAvgDfReportMarksReport():

print("The getBelowAvgDfReportMarksReport got invoked\n")

def getBelowAvgProjectMarksReport():

print("The getBelowAvgProjectMarksReport got invoked\n")

def getBelowAvgFinalExamMarksReport():

print("The getBelowAvgFinalExamMarksReport got invoked\n")

def getBelowAvgOverallMarksReport():

print("The getBelowAvgOverallMarksReport got invoked\n")

def displaySelectedStudentsMarks():

print("The displaySelectedStudentsMarks got invoked\n")

menu()

#-------------------End of week 6 Solution------------------------------------

Question 5:

Observe from the hierarchical diagram on page 1 that the enterAllMkarksOfAStudentToStorage function invokes the searchStudentMarks to ensure that there is no conflict with existing student names. That is once a new student name is entered we have to ensure that such a name does not already exist in the storage. The searchStudentMarks should return True if a new student name exists in the existing storage of all students and False if not. So this function would need two parameters, namely the student name as a key to search and the dictionary of all students records (to search from).

Write the definition of searchStudentMarks.
Question 6: Now update the enterAllMkarksOfAStudentToStorage such that it repeatedly asks for a new student name if an already existing name is entered.

#-----------------------------Week 7 Solution --------------------------------------------------------

def menu(): allStudentMarks = {} #Storage for all students marks. The storage is in the form of a dictionary data structure while True: kbinput = input("Enter 1 to store student details\n" + "Enter 2 to display student report\n" + "Enter any other key to exit\n") if kbinput == '1': allStudentMarks = enterAllMarksOfAStudentToStorage(allStudentMarks) elif kbinput == '2': displayAllReports() elif kbinput!='1' and kbinput != '2': break #break could have been replaced by return. return returns the control flow out of the function #break breaks the control out of the loop def enterAllMarksOfAStudentToStorage(allStudentMarks): while True: fname = input("Enter a student's full name\n") if searchStudentMarks(fname,allStudentMarks ): print(fname, " already exists in the system. Choose another name\n") else: break #enter valid marks for DF Report; valid marks range: 0 to 20 while True: dfReportMarks = input("enter marks for DF Report; valid marks range: 0 to 20\n") dfReportMarks = float (dfReportMarks) if dfReportMarks >= 0 and dfReportMarks <= 20: break #enter valid makrs for Project: valid marks range: 0 to 30 while True: projectMarks = input("enter marks for Project; valid marks range: 0 to 30\n") projectMarks = float (projectMarks) if projectMarks >= 0 and projectMarks <= 30: break while True: finalExamMarks = input("enter marks for Final Exam; valid marks range: 0 to 50\n") finalExamMarks = float (finalExamMarks) if finalExamMarks >= 0 and finalExamMarks <= 50: break studentMarks = [dfReportMarks, projectMarks, finalExamMarks] allStudentMarks[fname] = studentMarks ##temporary code to display current dictionary content for k, v in allStudentMarks.items(): print ("Student ", k, "'s marks are \n") for m in v: print(m, "\t") return allStudentMarks def displayAllReports(): kbinput = input("Enter 1 to get DF Report Marks\n" + "Enter 2 to get Project Marks\n" + "Enter 3 to get Final Exam Marks\n" + "Enter 4 to get Overall Marks\n" + "Enter 5 to get Selected Student Marks\n" + "Enter 6 to get to Menu\n") if kbinput == 1: getBelowAvgDfReportMarksReport() if kbinput == 2: getBelowAvgProjectMarksReport() if kbinput == 3: getBelowAvgFinalExamMarksReport() if kbinput == 4: getBelowAvgOverallMarksReport() if kbinput == 5: displaySelectedStudentsMarks() if kbinput == 6: return def getBelowAvgDfReportMarksReport(): print("The getBelowAvgDfReportMarksReport got invoked\n") def getBelowAvgProjectMarksReport(): print("The getBelowAvgProjectMarksReport got invoked\n") def getBelowAvgFinalExamMarksReport(): print("The getBelowAvgFinalExamMarksReport got invoked\n") def getBelowAvgOverallMarksReport(): print("The getBelowAvgOverallMarksReport got invoked\n") def displaySelectedStudentsMarks(): print("The displaySelectedStudentsMarks got invoked\n") def searchStudentMarks(newNameOfStudent, allStudentsStorage): return newNameOfStudent in allStudentsStorage menu()

#-----------------------------End of Week 7 Solution --------------------------------------------------------

Week 8 Practical Task

Question 7:

Observe from the hierarchical tree structure indicating the program flow in page 1,

that the displayAllReports function invokes the following functions

getBelowAvgDFReportMarksReport
getBelowAvgProjectMarksReport
getBelowAvgFinalExamMarksReport
getBelowAvgOverallMarksReport
displaySelectedStudentsMarks

This question requires you to complete the function definition of the displaySelectedStudentsMarks function to displays the details of a collection of selected students. Assume these details are passed as a parameter in the form of a dictionary object.

From the diagram on page 1 also observe that this function needs to invoke the displayAStudentsDetail function to display the details of one student which is passed as a parameter in the form of a dictionary object.

Hence also complete its function definition.

Question 8:

Now complete the function definition of the getBelowAvgDFReportMarksReport

function. Assume this function returns a list containing two elements. The first element is the average of the DF report marks of all students. The second element is a dictionary object containing the details of all students whose DF Report marks are below the average.

8