Project 10 of 13, for Paul
Grade Calculator
Marks for the class's first Python test in, averages and letter grades out, built from small functions that each do one job.
No account needed. Your work is kept in this browser until you sign up.
The brief
- PaulThe class sat its first Python test on Friday, and I've been putting off the marking.
- PaulThey're marked blind with candidate numbers instead of names, which means nobody gets a kind mark.
- PaulI want an average and a letter for each paper and for the class, with small functions I can check separately.
- NeilHe means he'll check every function. Twice, knowing him.
Write three functions, then a main() that uses them.
read_marks(text)takes marks typed with spaces between them, like"52 61 48", and returns them as a list of whole numbers:[52, 61, 48]. Nothing typed gives[].average(marks)returns the mean of a list of marks, orNoneif the list is empty.grade(mark)returns"A"for 70 or more,"B"for 60 or more,"C"for 50 or more,"D"for 40 or more, and"F"under 40.main()asksName?andMarks?for each paper until a blank name, and printsCandidate 1: 53.7 (C), orCandidate 3: no marks yetif no marks were typed. At the end it prints the average of every mark in the class,Class average: 70.0, orClass average: no marks yet.
Averages are shown to one decimal place.
Name? Candidate 1 Marks? 52 61 48 Candidate 1: 53.7 (C) Name? Candidate 2 Marks? 88 92 79 Candidate 2: 86.3 (A) Name? Candidate 3 Marks? Candidate 3: no marks yet Name? Class average: 70.0
What you learn
The lesson, step by step
A command of your own
A function is a named piece of work.
def greet():defines one: it teaches Python a new command, and the indented lines under it are what the command does.You write a job once and use it as often as you like, and you can check each job on its own. Paul's grade calculator will be three small functions.
Nothing under
defruns when Python reads it. It runs when you call the function, by writing its name with brackets:greet(). Each call runs those lines once. The example prints the greeting, Paul's line, then the greeting again.The usual slip is leaving off the brackets.
greeton its own only names the function. It doesn't run it.A new word
The bell for the end of class, as a function.
Something to work on
A parameter is a name in the brackets of a
def. Each call fills it with a value, so the function can work on something different each time.greet("Riya")runsgreetwithnameholding"Riya". The example printsMorning, Riya., thenMorning, Neil.A function can have several parameters, with commas between them. The values in the call fill them in order, first to first: with
def cost(coffees, price):, the callcost(4, 3.5)makescoffees4 andprice3.5.The usual slip is to think the names in the call have to match the parameters. They don't. Only the order counts.
First to first
Riya is labelling the pigeonholes. Read the last line carefully.
Handing the answer back
returnhands a value back to the code that called the function. The call then stands for that value, soeach = share(12.0, 4)puts3.0ineach.That's what lets functions build on each other: one works out a number, and other code uses it.
returnalso ends the function, there and then. Any lines after it don't run. The example prints3.0, then6.0, becauseshare(10.0, 2)gives back5.0and the last line adds 1.The usual slip is to
printwhere you meant toreturn. Printing shows a value to a person. The code that called the function gets nothing back.In and out
The coffee round for the class, as a function with two parameters.
Nothing to hand back
Noneis Python's word for no value at all. A function that finishes without reaching areturnhands backNone.You can use it on purpose. Paul's
averagewill returnNonefor a paper with no marks on it, because there's no average to give.find_roomonly returns for Paul. For anyone else it reaches the end with nothing to return, so the call givesNone. To ask whether something isNone, writeis None:room is NoneisTrueorFalse. The example printsThe Workshop,None, thenTrue.The usual slip is to expect
print(None)to print nothing. It prints the wordNone. A strayNoneon screen usually means a function printed when it should have returned.Printed, or handed back
Paul's remark on a score.
What stays inside
A variable made inside a function belongs to that function. It exists while the function runs and is gone when it ends. Where a name can be used is called its scope.
That keeps functions apart. One function's working-out can't get mixed up with another's, even when they use the same names.
tipis made insideadd_tip, so onlyadd_tipcan use it. The example prints44.0. Addprint(tip)as the last line and run it: a NameError, because outside the function there's notip.The usual slip is to expect a value made inside a function to be there afterwards. To get a value out,
returnit, and keep it with=where you call it.The point that went missing
Two variables called
score: one outside the function, and one in its brackets.How Paul would do it
- PaulThese are the printing costs for the class, split into two small jobs that each get their own name.
Paul's printing costs are two small functions and a
main(). Each small function does one job and returns its answer.main()does all the asking and printing.The last two lines mean: run
main()when this file is run, but not when another program borrows its functions. The checks borrow them, to test each one on its own. That's all you need to know about those lines for now.The labels beside the code are the steps he took. Your grade calculator takes the same steps for each of its functions.
Fix Caitlin's version
- CaitlinI did the grade bit for Paul and it puts the letter straight up on the screen, instantly.
- CaitlinBut then None turns up in the brackets and the C ends up on a line of its own. Can you look?
Run Caitlin's program. It should print exactly:
Candidate 1: 53.7 (C)Fix
gradeso it hands its letter back. The checks also callgradeon its own, with other marks.Make the grade calculator
Write
read_marks,average,gradeandmain(). The brief has the whole spec.Name? Candidate 1 Marks? 52 61 48 Candidate 1: 53.7 (C) Name? Class average: 53.7The checks call the first three on their own, with other values, then run a whole class through
main(). So those three mustreturntheir answers, and onlymain()prints.An empty list has no average, and Python won't divide by
len([]), which is 0. Check for it first and returnNone. Thenmain()asksis Nonebefore it prints.Two things you haven't needed before:
sum(marks)adds up a list of numbers, andevery_mark + marksjoins two lists into one.Ship it
- PaulCandidate 2, eighty-six point three. That's tidy code, whoever you are.
- YahyaWait, is Candidate 2 me?
- PaulIt's blind marking, Yahya, you'll find out on Friday.