Paul's Python CoursePPC

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.

About 70 minutes, in 15 steps, free and with no account needed.

Start the project

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, or None if 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() asks Name? and Marks? for each paper until a blank name, and prints Candidate 1: 53.7 (C), or Candidate 3: no marks yet if no marks were typed. At the end it prints the average of every mark in the class, Class average: 70.0, or Class average: no marks yet.

Averages are shown to one decimal place.

What it prints
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

  1. 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 def runs 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. greet on its own only names the function. It doesn't run it.

  2. A new word

    The bell for the end of class, as a function.

  3. 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") runs greet with name holding "Riya". The example prints Morning, Riya., then Morning, 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 call cost(4, 3.5) makes coffees 4 and price 3.5.

    The usual slip is to think the names in the call have to match the parameters. They don't. Only the order counts.

  4. First to first

    Riya is labelling the pigeonholes. Read the last line carefully.

  5. Handing the answer back

    return hands a value back to the code that called the function. The call then stands for that value, so each = share(12.0, 4) puts 3.0 in each.

    That's what lets functions build on each other: one works out a number, and other code uses it.

    return also ends the function, there and then. Any lines after it don't run. The example prints 3.0, then 6.0, because share(10.0, 2) gives back 5.0 and the last line adds 1.

    The usual slip is to print where you meant to return. Printing shows a value to a person. The code that called the function gets nothing back.

  6. In and out

    The coffee round for the class, as a function with two parameters.

  7. Nothing to hand back

    None is Python's word for no value at all. A function that finishes without reaching a return hands back None.

    You can use it on purpose. Paul's average will return None for a paper with no marks on it, because there's no average to give.

    find_room only returns for Paul. For anyone else it reaches the end with nothing to return, so the call gives None. To ask whether something is None, write is None: room is None is True or False. The example prints The Workshop, None, then True.

    The usual slip is to expect print(None) to print nothing. It prints the word None. A stray None on screen usually means a function printed when it should have returned.

  8. Printed, or handed back

    Paul's remark on a score.

  9. 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.

    tip is made inside add_tip, so only add_tip can use it. The example prints 44.0. Add print(tip) as the last line and run it: a NameError, because outside the function there's no tip.

    The usual slip is to expect a value made inside a function to be there afterwards. To get a value out, return it, and keep it with = where you call it.

  10. The point that went missing

    Two variables called score: one outside the function, and one in its brackets.

  11. 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.

  12. 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 grade so it hands its letter back. The checks also call grade on its own, with other marks.

  13. Make the grade calculator

    Write read_marks, average, grade and main(). The brief has the whole spec.

    Name? Candidate 1
    Marks? 52 61 48
    Candidate 1: 53.7 (C)
    Name?
    Class average: 53.7
    

    The checks call the first three on their own, with other values, then run a whole class through main(). So those three must return their answers, and only main() prints.

    An empty list has no average, and Python won't divide by len([]), which is 0. Check for it first and return None. Then main() asks is None before it prints.

    Two things you haven't needed before: sum(marks) adds up a list of numbers, and every_mark + marks joins two lists into one.

  14. 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.

Guides that go with it