You can write a second Python program from scratch.
That claim is different from finishing the money tracker. The tracker had a starter at every step. This page does not. You decide the steps, the names and the order. The sandbox below holds the class results and nothing else, on purpose.
What to build
Build a grade report for one university course, MTH 101. It is not part of your money tracker. It is a new program with its own job.
The test results arrive as a block of text, already in the sandbox. Each line holds a student's name, then three test scores out of 100:
Adaeze Okafor, 72, 65, 80One line is bad. A score was typed as a word instead of a number. Real data arrives like this. Your program must skip that line and say so, not crash.
When it works, your report shows four things:
- One line per student: their name, their average to one decimal place, and their letter grade.
- A class summary: how many students were graded, the class average, and the highest average.
- A ranking of the students, from the highest average down.
- The skipped lines, listed at the end.
Build it from these parts:
- Turn each line into a record: a dictionary with the student's name and their average. Use string methods to split the line and clean each part.
- Convert each score with
int. Put the conversions insidetry, and catch theValueErrorthat the bad line causes. - Write a function that takes a list of scores and uses
returnto hand back their average. - Work out each letter grade with
if,elifandelse. - Build the ranking with
sorted,key=andreverse=True. The order must come from the data, not from the order you typed names in.
The grades use the 5-point bands that many Nigerian universities use:
- A: 70 and above.
- B: 60 to 69.
- C: 50 to 59.
- D: 45 to 49.
- E: 40 to 44.
- F: below 40.
An average between two bands belongs to the lower one. 69.7 is below 70, so it is a B.
One new tool, given to you. An average is often a long decimal. round(number, 1) hands back the number rounded to one decimal place:
average = round(217 / 3, 1)
print(average)That prints 72.3. Without round, the same division prints 72.33333333333333. Nothing else in the track rounds, so use it exactly as shown. Everything else you need, you have written before.
You may open your money tracker while you work, and copy patterns from it. Real developers reuse code that works. From scratch here means a new program with no lesson steps, not a memory test.
How to know you are done
Run your program and check these outcomes yourself.
With the results as given, a finished report looks like this. Your title, labels and layout can be your own. The names, numbers, grades and order must match.
MTH 101 Grade Report
Students:
Adaeze Okafor: 72.3, grade A
Tunde Bakare: 60.7, grade B
Amina Bello: 52.0, grade C
Chinedu Eze: 46.7, grade D
Zainab Yusuf: 70.0, grade A
Funmilayo Adeyemi: 42.3, grade E
Ibrahim Musa: 35.7, grade F
Emeka Obi: 82.7, grade A
Students graded: 8
Class average: 57.8
Highest average: 82.7
Ranking:
Emeka Obi: 82.7
Adaeze Okafor: 72.3
Zainab Yusuf: 70.0
Tunde Bakare: 60.7
Amina Bello: 52.0
Chinedu Eze: 46.7
Funmilayo Adeyemi: 42.3
Ibrahim Musa: 35.7
Skipped lines:
Ifeoma Nwosu, 66, absent, 71-
Every good line gives one student line, with an average to one decimal place and a letter grade. With the results as given, that is eight students.
-
The bad line does not stop the program. Ifeoma Nwosu is missing from the student lines and the ranking, and her line appears under the skipped lines. If the run stops with an error instead, your
trydoes not cover the line that converts her scores. The error ends with this line:ValueError: invalid literal for int() with base 10: 'absent'If you convert a score without
strip(), the quotes show' absent', with a space in front. -
Zainab Yusuf averages exactly 70.0, which is an A. If your report gives her a B, check the comparison for that band. "70 and above" needs
>=, not>. -
Every average, the class average included, shows one decimal place. A long tail of digits, like
57.791666666666664, means a missinground. -
Fix the bad line: change
absentto60. Run again. Ifeoma now averages 65.7, a B, and ranks fourth. Students graded becomes 9, and the class average becomes 58.7. The skipped lines section has nothing left to list. -
Change any score of your own choosing, and run again. The averages, grades, summary and ranking all follow your change. None of them should need editing by hand.
Optional stretch (not required): count how many students got each grade, using a dictionary built in a loop. With the results as given, that is three A grades and one of every other grade. The report is finished without this. Add it only if you want the extra composition.
When you get stuck
These point back to earlier lessons by their titles in the lesson list. They do not re-teach the ideas.
- Text across several lines with
""", andsplitlines(),split(",")andstrip(): String Methods. - Turning
"72"into72withint: Types and Conversion. - Division that gives a decimal: Numbers.
- Lists,
appendandlen: Lists. sumandmax: sum, max and min.- A loop over every line, then over every student: for Loops.
- A function with parameters: Functions.
- Handing back an answer with
return: Return Values. >=and the other comparisons: Comparisons.- Bands with
if,elifandelse: if and else and elif. - Values inside text: f-strings.
- A dictionary, read with
record["name"]: Dictionaries. - A list of records, and pulling one field out of each into a plain list: Lists of Dictionaries.
- Skipping a bad line with
tryandexcept ValueError, then listing it at the end: try and except. - Ranking with
sorted,key=andreverse=True: Sorting. - The stretch's count per grade, with
inandelseinside a loop: Looping Over Dictionaries.
Your tracker's receipt loop already has the shape you need. It splits a line, converts inside try, and appends a record or the skipped line. Its amount_of function hands sorted one field of a record. Your averaging function is not tied to test scores, either. Give it the list of student averages, and it hands back the class average.
Build it here
You now have two finished programs: the money tracker the track built with you, and a grade report you built without the rails. That is the end of the Python track. Press DOWNLOAD to keep your report. It also saves as main.py. If your tracker's main.py is already in that folder, rename the tracker first, to something like tracker.py. Then run your report on your own computer as the last checkpoint showed.