Small application project

Advanced

The project four-step: requirements → design → implementation → testing. Design fixes the data structure (dictionary) and the division of functions (add, stats, save); testing covers normal and extreme input (text as score, empty dictionary, duplicate names).

For:Secondary 3 Information & Communication Technology · Unit:Programming II · Lesson 4 of 13 in the learning path

After this lesson, you can:
  • The project four-step: requirements → design → implementation → testing, in that order.
  • Design fixes two things first: the data structure (dictionary, name → score) and the function split (one job each).
  • Menus pair with while loops; remember input returns a string, so compare with == "1".
文體:專案指南課有聲朗讀

文體小知識:專案指南課由企劃到評估逐步帶你完成整個製作,適合接著做小組功課和比賽作品。

資訊及通訊科技Information & Communication Technology

§Text Passage

1Writing a real program does not start with typing code. The professional flow has four steps: requirements analysis (what to build, and for whom) → design (plan the data structure, the division of functions, the interface) → implementation (write in pieces, testing as you go) → testing (normal and extreme cases alike). Skip the first two and you will almost surely start over.

2Take the demo project 'score recorder'. Requirements analysis: classmates want a program to record test scores per subject, add new scores any time, view the average and highest, and keep the data after closing. Those three sentences are the requirements — each must be verified in testing later.

3Design stage: the data structure is a dictionary grades = {"Mary": 85, "John": 90}, keys as names and values as scores; the functions split into add_score(), show_stats(), save() and load(); the main program loops in while showing a menu so the user types 1 to 4 to pick a feature. Draw the design on paper and you will not get lost while coding.

4Implementation and testing: write one function at a time and try it immediately before moving on. Testing asks three questions: is normal input right (the average of 85 and 90 is 87.5)? What about extreme input (typing "abc" as a score, a duplicated name, averaging an empty dictionary)? Does reloaded data match what was saved? Only when all pass is the project done.

1Summary 重點摘要

The project four-step: requirements → design → implementation → testing. Design fixes the data structure (dictionary) and the division of functions (add, stats, save); testing covers normal and extreme input (text as score, empty dictionary, duplicate names).

2Vocabulary cards Vocabulary

R
Requirements analysis需求分析

The stage deciding what the program must do and for whom, ending with a clear feature list.

D
Design設計

Planning the data structure, the split of functions and the interface before coding.

I
Implementation編寫實作

Writing the code function by function following the design, testing as you go.

T
Testing測試

Verifying the program against requirements with normal and extreme inputs.

M
Menu選單

An interface listing numbered features for the user to pick, shown repeatedly inside a while loop.

I
Input validation輸入驗證

Checking that user input is sensible (e.g. a score must be a number) and asking again when not.

M
Modular design模組化設計

Splitting a program into small functions with single jobs, easy to write, test and change.

B
Boundary case邊界情況

The extremes most often overlooked, such as empty lists, zero scores or duplicated names.

D
Documentation文件說明

Recording what the program does, how to use it and why it was designed that way.

3Step by step Steps

  1. Step one: the requirements listWriting a real program does not start with typing code. The professional first step is the requirements list: every desired behaviour, one sentence each. For a 'score recorder': one, add a student's name and score; two, show the average and the highest score; three, data survives closing the program. Each line is later accepted or rejected — an unfinished line means the project is not done. Clear requirements anchor the design and testing that follow; skip this and halfway through you discover a missing feature and start over.
  2. Choosing the data structureStep two picks the data structure from the requirements. A score recorder needs 'look up a score by name', so a dictionary fits: grades = {"Mary": 85, "John": 90}, names as keys and scores as values. Adding a record is grades["Peter"] = 78; a lookup is grades["Mary"]; before statistics, grades.values() takes out every score. Recording events in order would want a list; a name-to-phone search wants a dictionary. The right structure makes every feature easy; the wrong one blocks everything.
  3. Dividing the work among functionsStep three assigns the work to functions, one job each: add_score(grades, name, score) adds a record; show_stats(grades) computes and prints the average and highest; save(grades) and load() handle the file. Single responsibility pays twice: when something breaks, the culprit function is found quickly, and changing how the average is computed touches only show_stats. Functions pass data through parameters and return results, while the main program merely directs the flow.
  4. The menu loop and inputThe main program runs a menu inside while True:: first print("1 add 2 stats 3 quit"), then choice = input("choose: "), then if choice == "1": add a score, elif choice == "3": break. The crucial detail: input() returns a string, so "1" is not the number 1 — compare with choice == "1", or convert with int(choice) for arithmetic. Forgetting this is the beginner's commonest bug: the program ignores the input 1 entirely, leaving everyone puzzled.
  5. Input validation: foolproofingUsers do not always follow the rules. If a user types "abc" as a score, int(text) raises an error and the program crashes. The fix is try/except: after text = input("score: "), write try: score = int(text) except ValueError: print("please enter a number") and continue back to the menu. Illegal input then draws a message instead of a crash. The foolproofing of every professional application and game grows from this idea: assume the user will err and block it in advance, so the experience stays smooth.
  6. Testing normal and extreme casesTesting splits in two. Normal cases: add 85 then 90 and choose stats, expecting an average of 87.5 and a highest of 90 — write the expected values on paper first and tick each off after running. Extreme cases: choosing stats with no records divides by zero unless len(grades) == 0 is checked first; a duplicated name must update rather than double-record; and after saving, closing and reopening, the data must be identical. Extreme cases expose the hidden bugs best and must all pass before delivery.
  7. When the project is finishedA project is finished not when 'it runs' but when three questions all pass: is normal input correct, does extreme input survive without crashing, does saved data reload unchanged? Then tick the requirements list line by line. The discipline of writing one function at a time and testing it immediately keeps debugging narrow; writing everything before testing lets errors tangle together. Requirements, design, piecewise coding and two-sided testing — this flow serves every programming project to come.

4Diagrams Diagrams

多媒體專題:五步由零到成品 Project steps1定主題目標觀眾係邊個2寫腳本故事版 storyboard3收集素材相・聲・片(版權!)4製作整合剪輯5測試發布人哋睇得明嗎最常見死因:冇腳本就開工——做到一半先知缺素材、次序亂分工貼士(小組):一人腳本一人素材一人剪輯——同步溝通最緊要完成後自查:訊息清楚?音量適中?有冇錯字?播放流暢?
Fig. 1: Multimedia project

5Worked example Example

Example: write show_stats(grades) for the score recorder, printing the average and highest score, and demo it with grades = {"Mary": 85, "John": 90, "Peter": 78}.

  1. Block the empty dictionary first: if len(grades) == 0: print("no records yet") and return, avoiding division by zero.
  2. Take out the scores: scores = list(grades.values()), giving [85, 90, 78].
  3. Compute: average = sum(scores) / len(scores) = 253 ÷ 3 ≈ 84.33 and highest = max(scores) = 90.
  4. Print: print("average", round(average, 1), "highest", highest), showing average 84.3 highest 90.
  5. Extra test: call once with the empty dictionary {} and confirm it prints 'no records yet' instead of crashing.
  6. Answer: show_stats blocks the empty dictionary first, then prints an average of 84.3 and a highest of 90 from [85, 90, 78] — the keys are the defensive opening check and round() for tidy decimals.

6Common mistakes Mistakes

✗ Typing code at once, working out the features as you go.
✓ Without requirements and design you discover halfway that the features are wrong and start over; ten minutes of planning saves hours.
✗ Dumping all the code into one big block for convenience.
✓ Split into small functions (add, stats, save separately) so faults are easy to locate and changes stay local.
✗ Assuming users always type numbers, so no checking needed.
✓ Real users type anything: "abc", negatives, blanks. Add try-except and range checks to survive them.
✗ Keeping data in variables only and retyping after each restart.
✓ Variables die with the program; save with open("grades.txt", "w") and load() on startup to keep data for good.
✗ Stopping once the normal case works — no extreme tests needed.
✓ Empty-dictionary averages, duplicated names, zero scores are where things break; professional testing covers both kinds.

7Quick check Quick check

Q1The correct order for building a small application? A. code → test → design → requirements B. requirements → design → code → test C. design → code → requirements → test

Q2In the score recorder, grades = {"Mary": 85, "John": 90}; after grades["Peter"] = 78, what is len(grades)?

Q3The user types "abc" as a score — which pattern keeps the program from crashing?

Q4Calling show_stats() on an empty grades dictionary — what goes wrong and how to handle it?

Q5Name any three checks when testing the score recorder.

8Recap Recap

  • The project four-step: requirements → design → implementation → testing, in that order.
  • Design fixes two things first: the data structure (dictionary, name → score) and the function split (one job each).
  • Menus pair with while loops; remember input returns a string, so compare with == "1".
  • Validate input with try-except catching ValueError so bad input never crashes the program.
  • Test normal and extreme cases alike: empty dictionary, duplicate names, save-and-reload all verified.

Other subjects in this grade

Same subject, other grades