Skip to content
ExamSpine

Lesson 3.4 · Write it

Average score

A whole programPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a program that reads test scores one at a time, stopping at -1, and prints their whole-number average.

  1. Read one score per line. Every score is a whole number from 0 to 100.
  2. The value -1 means "no more scores". It is not a score.
  3. If at least one score was entered, print Average: followed by the average as a whole number (drop the fraction).
  4. If -1 is typed first (no scores at all), print No scores.

In Python, use // for whole-number division. In Java, / between two ints already drops the fraction.

Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.

  • three scores, average is 80 (rule 3)

    You type

    1. typed: 80
    2. typed: 70
    3. typed: 90
    4. typed: -1

    It prints

    Average: 80
  • no scores at all (rule 4)

    You type

    1. typed: -1

    It prints

    No scores
  • one score (rule 3)

    You type

    1. typed: 50
    2. typed: -1

    It prints

    Average: 50
solution.pySolution.java