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.
- Read one score per line. Every score is a whole number from 0 to 100.
- The value
-1means "no more scores". It is not a score. - If at least one score was entered, print
Average:followed by the average as a whole number (drop the fraction). - If
-1is typed first (no scores at all), printNo 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
- typed: 80
- typed: 70
- typed: 90
- typed: -1
It prints
Average: 80
no scores at all (rule 4)
You type
- typed: -1
It prints
No scores
one score (rule 3)
You type
- typed: 50
- typed: -1
It prints
Average: 50