From Working Code to Program Design · capstone
Capstone project: Project: Describe a Better Design
This project improves the design with familiar tools.
The starting program uses parallel
prompts = [
"Two plus two?",
"Three plus three?",
]
answers = ["4", "6"]
points = [1, 2]
Position zero in each list describes one question. Position one describes another. That relationship exists only in the programmer’s understanding.
If one list is reordered or one item is omitted, the records silently stop matching.
Your function-first redesign
Refactor the program around three small public
make_question(prompt, answer, points)
is_correct(question, response)
score_quiz(questions, responses)
The resulting list should contain complete question
questions = [
make_question("Two plus two?", "4", 1),
make_question("Three plus three?", "6", 2),
]
score_quiz returns the total points earned. It must use the behavior in is_correct rather than repeating the answer comparison.
Keep the responsibilities focused
The previous lesson introduced a responsibility as a promise one part of the program owns. This design gives each function one:
make_questionowns creation of a complete record.is_correctowns answer comparison.score_quizowns the quizand total.
The construction responsibility includes the rules already established in this chapter: prompts and answers cannot be blank, and points must be at least 1.
This structure is better than the parallel lists and may be all a modest program needs.
Notice what improved
Before the refactor, the program depended on matching positions across three lists. After the refactor:
each question is one complete
; every question is created in the same way;
one function contains the answer-comparison rule;
the quiz loop focuses on scoring.
Nothing here requires unfamiliar syntax. Program design often starts by arranging familiar pieces so their jobs are clear.
Why is a list of complete question dictionaries safer than three parallel lists?
After your project passes, describe one improvement and one cost of the refactor in your own words. The reflection is for your design judgment and is not hidden in the grader.
Task
Replace the parallel-make_question, is_correct, and score_quiz.
Requirements:
each question is one
with prompt,answer, andpoints;make_questionrejects blank prompts, blank answers, and points below 1 with; answer checking ignores surrounding spaces and letter case;
score_quizcallsis_correctinstead of repeating the comparison;correct responses add that question’s points;
incorrect responses add zero.
The provided program should print Score: 3.