needmoreeasy EN 한국어
Learn

32 — Grade book: a score per name, and an average

★★★★☆ (4/5)projects
Prerequisites
30 — Shop, 31 — Bank
You will end up with
a grade book holding a score per name, reporting the average and the best

A grade book is one score under each name. But working out an average needs the scores on their own — a record has no order and no total. So the record is walked and the scores are copied into a list.

Steps

  1. Put the names and scores in a record:

    set marks to an empty record
    put Mina at 88 in marks
    put Sana at 92 in marks
    show Mina in marks
    
    run it →

    You get 88.

  2. Walk it and collect the scores into a list. Only a list can be asked for a total and a count:

    set marks to an empty record
    put Mina at 88 in marks
    put Sana at 92 in marks
    set scores to an empty list
    for each name in marks
        set score to name in marks
        append score to scores
    end
    show how many scores
    show the total of scores
    
    run it →

    2 and 180.

  3. The average is the total divided by the number of people. Put the divisor in a name first:

    set scores to list of 88, 92
    set people to how many scores
    set average to the total of scores
    divide average by people
    show average
    
    run it →

    You get 90.0. What you divide by has to be a name or a number — divide average by how many scores is not a thing, which is why people holds it first.

  4. The best score the list answers directly:

    set scores to list of 88, 92, 75
    show the biggest of scores
    
    run it →

    You get 92.

  5. The whole thing:

    set marks to an empty record
    put Mina at 88 in marks
    put Sana at 92 in marks
    put Ada at 75 in marks
    set scores to an empty list
    for each name in marks
        set score to name in marks
        append score to scores
        show name
        show score
    end
    set people to how many scores
    set average to the total of scores
    divide average by people
    show average
    show the biggest of scores
    
    run it →

Try it yourself

Pick out everyone above the average — walk the record a second time and show the name when if score is greater than average. It has to be a second walk: during the first one the average does not exist yet.

What you learned

  • A record is for looking up by name; a list is for totals, counts and the biggest.
  • Walking a record and copying its values into a list gets you both.
  • The average is the total divided by the count, and the divisor must be a name or a number.
  • A comparison against an average happens after the average is finished.

This page on GitHub