NCEA L1 Digital Tech study materials
NCEA L1 Digital Tech study resources

Quiz app fails at the open day

NCEA L1 Digital Tech · Put your knowledge to work in context.

All case studies

Quiz app fails at the open day

At the school open day, a Year 11 student's Python quiz runs on a laptop in the library. The quiz reads 40 questions from questions.txt in the format question|answer, shows them one at a time, and keeps a running score. In testing, three problems appear. (1) After question 10 the score shown drops back to 0 even though the player has answered correctly. (2) If the player types 'four' instead of '4', the program crashes with a ValueError. (3) Two players who finish at the same time have their final scores written to scores.txt, but only one line survives. The student has 90 minutes before the first visitors arrive and can only fix two issues reliably. The room's wifi is unreliable, so the quiz must run offline.

1. Which two of the three problems should the student fix before the open day, and which one can wait? Justify your choice using the effect of each problem on the user and on the fairness of the quiz.

Read the worked answer

The crash is the first priority because it stops the quiz completely. A visitor who types 'four' sees a Python traceback and cannot continue, so the student has to restart the program in front of an audience. The score reset is second because it destroys trust in the result: a player who has answered correctly sees 0 and concludes the quiz is broken. The lost score line is the least urgent because it only affects the saved record at the end, only when two players finish at the same moment, and the scores can still be read from the screen.

A useful way to judge is severity × likelihood ÷ cost to fix. The crash is certain to happen with open-day visitors, and the fix is small: a try/except around int(). The score reset is probably a score variable being set to 0 inside the loop, also a small, contained fix. The file conflict needs an append or locking change, which is the riskiest change to make in 90 minutes. Fixing the two small, certain failures gives the most reliable experience for the visitors.

2. The crash happens because the program calls int() on the player's input. Describe two different ways to stop the ValueError, and explain which is better for this quiz.

Read the worked answer

Option A: wrap the conversion in try/except. Ask for input, then try int(answer); if a ValueError occurs, print a short message and ask again without advancing the question or changing the score. Option B: validate the string before converting - strip whitespace and check answer.isdigit() - and repeat the prompt if the check fails.

Both keep the quiz running, but only if the input is inside a loop; otherwise the program still exits after the message. Option A is better here because it catches any conversion failure in one place and is easy to extend to other bad input. Option B gives a clearer message for the common case and is easier for a beginner to read, but isdigit() rejects '+4' and accepts some non-ASCII digits, which can surprise you.

For this quiz, use try/except inside a while loop. Test with '', 'four', '4.0', ' 4 ' and '-4' before the doors open, because open-day visitors will type all of these.

3. The quiz may later need questions with more than one correct answer. Explain one change to the file format and one matching change to the program, and state a limitation of the current format.

Read the worked answer

The current format assumes exactly one answer and no pipe characters in either field. Splitting a line on '|' gives exactly two parts, so any extra field shifts the data and the comparison fails. It also cannot represent two acceptable answers for one question.

One change is to keep the pipe as the field separator but store answers as a semicolon-separated list: question|answer1;answer2;answer3. The program splits each line on '|', splits the second field on ';', strips each item, and checks whether the player's input matches any item in the list. The matching change is to replace the equality test with a membership test.

The limitation is that the format breaks if an answer itself contains '|' or ';'. A more robust alternative is JSON, where each question is an object with a list of answers, so quoting and punctuation are handled correctly. JSON is worth the extra setup if non-programmers will edit the file; the semicolon version is quicker for 40 questions.