The exercise is about the following topics: Using lists Defining and calling functions Using function parameters and return values Using constants Lists are covered in chapters5.1and5.2. Functions are...


The exercise is about the following topics:




  • Using lists

  • Defining and calling functions

  • Using function parameters and return values

  • Using constants

Lists are covered in chapters5.1and5.2. Functions are covered insection 4. Pay attention particularly to the functions returning several values, in chapter4.3


Background


Let's return to the Finnish school environment for a while and focus on a program that processes school grades of different subjects and courses.


Initial preparations


Create a new project in Eclipse with the name assignment5_5. Then in this project create a module named for example "school_grades". (the name of the module doesn't matter when submitting the exercise). At the beginning of the module, copy the following constant lists:


GRADES = [4,5,6,7,8,9,10] GRADES_DESCRIPTIONS = ["fail", "tolerable", "decent", "satisfactory", "good", "excellent", "outstanding"]


Description of the exercise


Write a program that asks the user for school subjects, number of courses in each subject and grades of each course. The program prints a summary of the courses: the average of all courses and the average of all subjects as well as the strongest and the weakest subject.


Structure of the program


Write the following functions into your program:




  • input_course_grades_and_calculate_average(subject, number_of_courses)


    • subjectis the name of the subject (string), eg. "Physics".


    • number_of_coursesis the number of courses in that subject (integer).

    • the functions asks the user, according to the example printouts, the grade of each course. The function calculates and returns the (simple arithmetic) average of these courses. (This means we lose the information of the per-course grades, but that does not matter, as we do not examine them in detail in this program.)






  • calculate_averages(grade_average_list, course_amount_list)


    • grade_average_listis a list (of decimal numbers) containing the averages of each subject.


    • course_amount_listis a list (of integers) containing the number of courses of each subject

      • The courses are in the same order in both lists.



    • The function calculates and returns 2 values: the average of all subjects (ie. the average of all averages) and the average of all courses, returned in this order.

      • For instancecalculate_averages([5.0, 9.0], [3, 1])would return the values7.0, 6.0because


        • (5.0 + 9.0) / 2equals7.0


        • (5.0 * 3 + 9.0 * 1) / (3+1)equals6.0










  • describe_grade_average(grade_average)


    • grade_averageis a decimal number representing some grade average.

    • The function returns a verbal description (a string) based on which grade is the closest to the average according to the following table:


































      GradeDescription
      4fail
      5tolerable
      6decent
      7satisfactory
      8good
      9excellent
      10outstanding
      It is recommended to use the constant listsGRADESandGRADES_DESCRIPTIONShere. Some decimal numberxcan be rounded to a whole number by writingn = round(x, 0)after whichnis now the rounded integer (0 is the desired number of digits in the rounded numbern).
      You can expect the parameter to be between 4.0 and 10.0.
      NB! To make the exercise easier, round the number asround(x, 0)does by default. Do not use any other rounding methods at this point. Floating-point values are saved in the memory in a way that unintuitively causes the exact half numbersa.5to be always rounded down with evena's. and up with odda's. A better data type does exist, namely Decimal, which is always an exact decimal representation avoiding the problem above. Nevertheless, we don't use it in this exercise to make it simpler and without the need to learn a new data type.
      For instancedescribe_grade_average(8.33)ordescribe_grade_average(7.5)would both return the string'good'anddescribe_grade_average(7.0)ordescribe_grade_average(6.7)would return'satisfactory'but for instancedescribe_grade_average(6.5)would return'decent'due to the problem presented above.






  • find_strongest_and_weakest_subject(subject_list, grade_average_list)


    • subject_listis a list of subject names (strings).


    • grade_average_listis a list of the averages of those subjects (decimal numbers).

      • The courses are in the same order in both lists.



    • The function finds the strongest subject (the one with the highest average) and the weakest subject (the one with the lowest average) from the data and returns 4 values:

      1. The average of the strongest subject

      2. The average of the weakest subject

      3. The name of the strongest subject

      4. The name of the weakest subject



      For instance the callfind_strongest_and_weakest_subject(['Math', 'English', 'Biology', 'Chem'], [8.0, 9.5, 7.667, 7.25])would return9.5, 7.25, 'English', 'Chem'.
      In case the data contains several subjects with equal averages, it is sufficient to return just the first one in the lists. So, for instancefind_strongest_and_weakest_subject(["Music", "Arts", "Geography", "Psychology"], [7.6, 7.6, 8.35, 8.35]))would return8.35, 7.6, 'Geography', 'Music'



NB! The order of function parameters and return values as well as names of the functions must be exactly as presented above or else the grader will not give you points. (However, the names of the parameters and return values do not matter and can be anything in principle).


You can write other functions as well if you wish, but the functions above must be implemented as presented above.


In addition, write a main program which calls these functions when needed and runs the program as in the "Progression of the program" presented below. See also the example runs.


Progression of the program


The program asks the user for school subjects. For each subject, the program asks for how many courses the user attended in this subject. The program uses the functioninput_course_grades_and_calculate_averageto ask the grade for each course. The program saves the subject names, their course amounts and subject grade averages each on their own list. The length of all the three lists will be the same: the number of courses.


The subjects are asked until the user inputs an empty line when inputting the subject. After that, the program



  • uses the functioncalculate_averagesto calculate the course average and the subject average.

  • uses the functionfind_strongest_and_weakest_subjectto find out the user's strongest and weakest subjects and prints them and their averages

So, the following summary is printed for the user (se example runs):
Average of all subjects is [subject average] Average of all courses is [course average] The strongest subject is [strongest subject] with average [strongest subject's average] The weakest subject is [weakest subject] with average [weakest subject's average]
Always when printing a grade average, the program also prints a verbal description of that average using the functiondescribe_grade_average. This is printed in parenthesis after the grade average, as in the example runs.



Output formatting


All averages are printed with the precision of 1 decimal.


Pay close attention that the output of your program is approximately according to the example execution below (letter case, line breaks, spaces, full stops, commas, exclamation points or question marks are not checked even though they might be highlighted in the test feedback.)


Error handling


Your program does not need to handle errors: you can except the user to give only reasonable and valid input.


Your program also does not need to handle the situation where the user would not enter any subjects or would enter 0 as the course amount or something else as a positive integer. You can expect the given grades to be between 4 and 10 without checking them specifically.


Submitting


When you have written your program, run it with a couple of different inputs in Eclipse or in the environment you are using, and check that the output of the program is correct. Submit the fileschool_grades.pyto A+.


Examples of the execution of the program:



[execution of the program begins] The program calculates averages and analyzes your school performance. Enter the next subject: Biology How many courses of Biology did you attend? 7 Grade received for the course Biology 1: 9 Grade received for the course Biology 2: 8 Grade received for the course Biology 3: 9 Grade received for the course Biology 4: 9 Grade received for the course Biology 5: 8 Grade received for the course Biology 6: 9 Grade received for the course Biology 7: 9 Enter the next subject: History How many courses of History did you attend? 1 Grade received for the course History 1: 5 Enter the next subject: Civics How many courses of Civics did you attend? 3 Grade received for the course Civics 1: 5 Grade received for the course Civics 2: 7 Grade received for the course Civics 3: 6 Enter the next subject:  Average of all subjects is 6.6 (satisfactory) Average of all courses is 7.6 (good) The strongest subject is Biology with average 8.7 (excellent) The weakest subject is History with average 5.0 (tolerable) [execution of the program ends]



[execution of the program begins] The program calculates averages and analyzes your school performance. Enter the next subject: Health Education How many courses of Health Education did you attend? 2 Grade received for the course Health Education 1: 7 Grade received for the course Health Education 2: 9 Enter the next subject: Finnish How many courses of Finnish did you attend? 3 Grade received for the course Finnish 1: 9 Grade received for the course Finnish 2: 10 Grade received for the course Finnish 3: 9 Enter the next subject: Chemistry How many courses of Chemistry did you attend? 1 Grade received for the course Chemistry 1: 8 Enter the next subject: English How many courses of English did you attend? 2 Grade received for the course English 1: 6 Grade received for the course English 2: 7 Enter the next subject: Mathematics How many courses of Mathematics did you attend? 4 Grade received for the course Mathematics 1: 10 Grade received for the course Mathematics 2: 10 Grade received for the course Mathematics 3: 8 Grade received for the course Mathematics 4: 10 Enter the next subject:  Average of all subjects is 8.3 (good) Average of all courses is 8.6 (excellent) The strongest subject is Mathematics with average 9.5 (outstanding) The weakest subject is English with average 6.5 (decent) [execution of the program ends]



Oct 16, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here