HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Problems
  2. /This program works, but it is hard to maintain…
EasyComputer ScienceHigh School (9–10)Computer Science › ProgrammingcodingIGCSE

This program works, but it is hard to maintain…

StemPy LabContributed by StemPy Lab

Try it on paper first. A hint, the key idea and the worked solution are here when you need them.

This program works, but it is hard to maintain.

pseudocode
DECLARE X : INTEGER
DECLARE Y : INTEGER
DECLARE Z : INTEGER
INPUT X
Y ← 0
Z ← 0
WHILE X <> -1 DO
    Y ← Y + X
    Z ← Z + 1
    INPUT X
ENDWHILE
OUTPUT Y / Z

It reads game scores until −1-1−1 is typed, then outputs the average. Rewrite it so that it is easy to maintain, and correct one problem that happens when the first value typed is −1-1−1.

Need a reminder? The key ideaShowHide

A maintainable program uses meaningful identifiers, comments, constants for values that may change, and procedures and functions for separate jobs. In the 15-mark scenario question, list every task, plan the data, write the program in commented parts, and dry-run it.

The worked solution

Have you tried it?

Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.

A maintainable version.

pseudocode
// Works out the average score of a game, ended by the rogue value -1
CONSTANT EndValue ← -1
DECLARE Score : INTEGER
DECLARE TotalScore : INTEGER
DECLARE NumberOfScores : INTEGER
TotalScore ← 0
NumberOfScores ← 0
INPUT Score
WHILE Score <> EndValue DO
    TotalScore ← TotalScore + Score
    NumberOfScores ← NumberOfScores + 1
    INPUT Score
ENDWHILE
// Only divide when at least one score was typed
IF NumberOfScores > 0
  THEN
    OUTPUT "Average score ", TotalScore / NumberOfScores
  ELSE
    OUTPUT "No scores"
ENDIF

What changed. The identifiers say what they hold. The value −1-1−1 is a constant with a name. Comments explain the purpose and the check. The check stops a division by zero: with only −1-1−1 typed, the old program divided by 0.

Check. With the inputs 12, 9 and −1-1−1, the output is Average score 10.5. With only −1-1−1, it is No scores.

Log in to save this for later
← All Problems
← A year is a leap year when it divides exactly by 4, except that a year dividing by 100 is a leap…This function should return the larger of two integers, and the main program should output twice… →
More onComputer Science › Programming

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Maintainable programs and the long scenario questionProcedures, Functions and Strings in Pseudocode

More on this topic

  1. A long scenario question. A teacher has the names of 5 students in the file Names.txt, one name a…Hard
  2. Shopping.txt holds six lines: bread, rice, milk, eggs, tea and jam…Medium
  3. The file Best.txt holds one line: 120. What is output, and what does Best.txt hold afterwards, when…Easy
The nine squares of a board are input row by row: X, O, O, then X, X, X, then O, a dot for an empty…Hard
  • Three teams play three games each. Points[Team, Game] holds the points of a team in a game…Medium
  • All problems on this topic →