📚 GCSE WJEC Computer Science: Practical Programming Guide | GCSE WJEC 计算机科学:编程实验操作指南
In the WJEC GCSE Computer Science specification, the practical programming component challenges you to design, write, test and refine code using a high-level language. This guide walks you through the essential practical skills you must master – from setting up your environment to translating pseudocode – so you can tackle Unit 2 with confidence.
在 WJEC GCSE 计算机科学大纲中,编程实践部分要求你使用高级语言设计、编写、测试并完善代码。本指南将带你掌握从环境搭建到伪代码转换等必修实验技能,助你从容应对 Unit 2 的挑战。
1. Setting Up Your Programming Environment | 设置编程环境
Before you write a single line of code, you need a reliable programming environment. For WJEC, Python 3 is strongly recommended because it is clear, widely supported and used in most past practical tasks. Install the latest version from python.org, then choose an IDE that suits you: the standard IDLE, the beginner-friendly Thonny, or a richer editor like VS Code. Online platforms such as Replit can also be used and require no installation, but make sure you can save and submit files according to your school’s requirements.
在你写下第一行代码之前,你需要一个可靠的编程环境。WJEC 强烈推荐使用 Python 3,因为它语法清晰、支持广泛,并且大多数旧真题都用它。请从 python.org 下载最新版本,然后选择一个适合你的 IDE:标准 IDLE、新手友好的 Thonny 或功能更强的 VS Code。在线平台如 Replit 也无需安装即可使用,但必须确保能根据学校要求保存并提交文件。
Once installed, check that you can run a simple script that prints a message. This confirms your interpreter, paths, and editor are working correctly and avoids last-minute panic in the classroom.
安装后,请务必测试运行一条打印信息的简单脚本。这一步能确认解释器、路径和编辑器都正常工作,避免课堂上临时手忙脚乱。
2. Variables and Data Types | 变量与数据类型
Every program works with data, and variables are the labelled boxes you use to store that data. Python automatically determines the data type when you assign a value. The four fundamental types you will rely on are int (whole numbers, e.g. level = 3), float (decimals, e.g. temp = 36.6), str (text, e.g. name = 'Ada') and bool (True/False).
每个程序都要处理数据,变量就是你用来存放数据的带标签的盒子。Python 在赋值时会自动判断数据类型。你经常使用的四种基本类型是:int(整数,如 level = 3)、float(小数,如 temp = 36.6)、str(文本,如 name = 'Ada')和 bool(True/False)。
Converting between types is essential when you receive input: int('42') turns a string into a number, while str(100) turns a number into a string for printing. Always use meaningful variable names that follow lower_case_with_underscores style, and remember that both ‘single’ and “double” quotes work identically for strings in Python.
类型转换在处理输入时非常关键:int('42') 能把字符串变成数字,而 str(100) 能把数字变成可用于打印的字符串。请务必使用有意义的变量名,遵循 lower_case_with_underscores 风格,同时记住 Python 中单引号 ‘ 和双引号 ” 对字符串的作用完全等价。
3. Input and Output | 输入与输出
Interactive programs rely on getting data from the user and displaying results. The input() function displays a prompt and waits for the user to type something, always returning a string. You will almost always need to convert that string to another type: age = int(input('Enter your age: ')). Output uses print(), which can display multiple items separated by commas and automatically adds a space between them.
交互式程序需要从用户获取数据并显示结果。input() 函数会显示提示信息并等待用户输入,它始终返回字符串。绝大多数情况下你都需要将其转换为其他类型:age = int(input('Enter your age: '))。输出则使用 print(),它可以显示多个由逗号分隔的项,并自动在项之间添加空格。
A common exam technique is to output the result of a calculation in a formatted string. Using f-strings is the cleanest approach: print(f'Total cost: £{price * quantity:.2f}'). This keeps your code readable and reduces type-conversion errors.
考试中常用的技巧是用格式化字符串输出计算结果。使用 f-string 是最简洁的方法:print(f'Total cost: £{price * quantity:.2f}')。这既保持代码可读性,又减少了类型转换错误。
4. Making Decisions with Selection | 使用选择结构做决策
Selection allows your program to choose different paths based on conditions. The if statement tests a Boolean expression; if it is True, the indented block runs. You can extend this with elif (else if) and a final else. Relational operators ==, !=, <, >, <=, >= and logical operators and, or, not help you build complex tests.
选择结构能让程序根据条件走不同路径。if 语句检测布尔表达式,如果为 True,就运行缩进代码块。你可以用 elif(else if)和末尾的 else 进行扩展。关系运算符 ==, !=, <, >, <=, >= 与逻辑运算符 and, or, not 能帮你构建复杂检测条件。
In WJEC tasks, you will often need nested selection, for example checking if a user is logged in and then checking their access level. Always test with boundary values such as exactly 0, exactly 100, and one above or below a threshold. A common mistake is using = instead of == for comparison.
在 WJEC 任务中,你经常需要嵌套选择,例如先检查用户是否登录,再检查其访问级别。务必使用边界值测试,比如恰好为 0、恰好为 100 以及比阈值高或低一个单位。常见错误是用 = 代替 == 进行比较。
5. Repeating Code with Iteration | 使用迭代重复代码
Iteration lets you run a block of code multiple times. A for loop steps through a sequence, often created with range(start, stop, step). For example, for i in range(5): repeats five times with i taking the values 0 to 4. A while loop repeats as long as a condition remains True, which is ideal when you do not know the iteration count in advance.
迭代让你可以多次运行同一段代码。for 循环遍历一个序列,该序列通常用 range(start, stop, step) 创建。例如 for i in range(5): 会重复五次,i 依次取值 0 到 4。while 循环则当条件保持 True 时一直重复,适用于事先不知循环次数的情况。
Use break to exit a loop early when a special condition is met, and continue to skip the remainder of the current iteration. Be extremely careful with while True — always ensure there is a reachable break condition to avoid infinite loops that freeze your program.
用 break 可在满足特定条件时提前退出循环,用 continue 则跳过当前迭代剩余部分。对 while True 要极其小心 —— 始终确保存在可到达的退出条件,避免造成死循环、卡死程序。
6. Working with Lists and Strings | 操作列表和字符串
Lists store ordered collections of items, and they are mutable (can be changed). You create a list with square brackets: scores = [12, 8, 15]. Common methods include append() to add an item, remove() to delete a specific value, and len() to find the length. Indexing starts at 0, so scores[0] accesses the first element.
列表存储有序集合,并且是可变的(可以修改)。你用方括号创建列表:scores = [12, 8, 15]。常用方法有:用 append() 添加元素,用 remove() 删除特定值,用 len() 获取长度。索引从 0 开始,所以 scores[0] 访问第一个元素。
Strings are technically immutable sequences of characters but share many list-like behaviours. You can slice with word[0:3] or use methods like .upper(), .lower() and .split(). Looping through a string with for char in text: is a frequent requirement in WJEC coding challenges.
字符串在技术上是不可变的字符序列,但共享很多类似列表的行为。你可以用 word[0:3] 进行切片,或使用 .upper()、.lower() 和 .split() 等方法。用 for char in text: 循环遍历字符串是 WJEC 编程挑战中的常见要求。
7. Creating Reusable Functions | 创建可复用函数
Functions let you name a block of code and reuse it. In Python you define a function with def function_name(parameters):. Parameters allow data to be passed in, and the return statement sends a value back. Functions without a return statement implicitly return None. Well-designed functions each do one clear job, making your code modular and easier to test.
函数让你给一段代码命名并重复使用。在 Python 中,你用 def function_name(parameters): 定义函数。参数用来传入数据,return 语句把值传回去。没有 return 语句的函数隐式返回 None。设计良好的函数各自只做一件明确的事,这让代码模块化且更易于测试。
In the WJEC NEA or practical exam, you will need to write several independent functions and then call them from a main routine. Remember that variables inside a function are local; they do not affect variables with the same name outside the function. Practise writing functions that validate input, perform calculations, and format output.
在 WJEC 的非考试评估或实践考试中,你需要编写若干独立函数,然后从主程序调用它们。记住,函数内部的变量是局部的,不影响函数外部的同名变量。多练习编写验证输入、执行计算和格式化输出的函数。
8. Reading and Writing to Files | 读写文件
Many WJEC programming tasks require handling external text files. The safest way is to use a with open('filename.txt', 'r') as f: structure, which automatically closes the file when the block ends. Common file modes are 'r' for reading, 'w' for writing (overwrites existing content), and 'a' for appending.
许多 WJEC 编程任务要求处理外部文本文件。最安全的方式是使用 with open('filename.txt', 'r') as f: 结构,它在代码块结束时自动关闭文件。常见文件模式有:'r' 读取,'w' 写入(会覆盖已有内容),'a' 追加。
For reading, f.read() gets the whole file as a string, while f.readline() gets one line at a time. When writing, you must supply a string to f.write(). Always anticipate that a file might be missing and use exception handling (try...except) to avoid crashes.
读取时,f.read() 将整个文件作为字符串返回,而 f.readline() 每次读取一行。写入时必须向 f.write() 提供字符串。请始终预见到文件可能缺失的情况,并使用异常处理 (try...except) 避免程序崩溃。
9. Debugging and Testing Your Code | 调试与测试代码
Even experienced programmers introduce errors, so debugging systematically is a core skill. Syntax errors (such as missing colons) stop the program running and Python gives a line number. Runtime errors (e.g. dividing by zero) crash the program during execution. Logic errors produce wrong results without crashing, and are hardest to spot.
即使是经验丰富的程序员也会引入错误,因此系统调试是一项核心技能。语法错误(如缺少冒号)会阻止程序运行,Python 会给出行号。运行时错误(如除以零)会在执行中让程序崩溃。逻辑错误则产生错误结果却不崩溃,最难发现。
Develop a testing table with inputs, expected outputs, and actual outputs. Test normal data, extreme (boundary) data, and erroneous data. Use temporary print() statements to inspect variable values, or learn to set breakpoints in your IDE. Remember the WJEC markscheme rewards evidence of testing, so keep screenshots or written tables.
制作一张测试表格,包含输入、预期输出和实际输出。测试正常数据、极端(边界)数据以及错误数据。使用临时 print() 语句检查变量值,或学会在 IDE 中设置断点。请记住,WJEC 评分方案奖励提供测试证据,所以要保留截图或填好的表格。
10. Translating Pseudocode into Python | 将伪代码转换为 Python
WJEC exam papers often present pseudocode that you need to convert into working Python. The table below maps common pseudocode constructs to their Python equivalents. Practice transcribing pseudocode exactly, keeping variable names identical to those in the question.
WJEC 试卷经常给出需要转换成有效 Python 代码的伪代码。下表列出了常见伪代码结构与 Python 语法的对应关系。请练习逐句转录伪代码,并保持变量名与题目完全一致。
| Pseudocode | 伪代码 | Python Code | Python 代码 |
|---|---|
OUTPUT 'Hello' |
print('Hello') |
INPUT name |
name = input() |
IF score > 50 THEN |
if score > 50: |
FOR i ← 1 TO 10 |
for i in range(1, 11): |
WHILE x < 10 DO |
while x < 10: |
FUNCTION calcArea(width, height) |
def calcArea(width, height): |
RETURN area |
return area |
Notice that the pseudocode FOR i ← 1 TO 10 is inclusive of 10, so you need range(1, 11) in Python. Also pay attention to indentation: in pseudocode blocks are indicated by keywords like ENDIF or NEXT i, but in Python you must indent each block consistently using 4 spaces.
注意,伪代码 FOR i ← 1 TO 10 包含 10,所以 Python 中要用 range(1, 11)。还要注意缩进:伪代码中用 ENDIF 或 NEXT i 等关键字标明代码块,但在 Python 中你必须用 4 个空格统一缩进每个代码块。
Always cross-check the translated output against a sample trace table. This verifies that your loops, conditions and variable updates match the original design intent.
始终用示例追踪表交叉检查转换后的代码。这能验证循环、条件和变量更新是否符合原设计意图。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导