让电脑听懂你写的程序Help the computer understand your program

Python 组 · Windows

我做I am doing 电脑是My computer 语言Language

我做到了 0 / 0

全班分成两组。你只做自己这一组,一共 6 关。前 4 关只用记事本和命令行,后 2 关才请 VS Code 来帮忙。每关最后有一个绿色或蓝色的「通关对照」。你的屏幕和它一致,再打勾。The class splits into two groups. Do only your own group. There are 6 levels. Levels 1 to 4 use a plain text editor and the command line. VS Code joins at levels 5 and 6. Each level ends with a green or blue Pass check. Tick the box when your screen matches it.

Python 和 C++ 都是高级语言,人读得懂。Python 用解释器:说一句,做一句。C++ 用编译器:整篇翻译完,再交给你一个能运行的程序。Python and C++ are both high-level languages: people can read them. Python uses an interpreter: it does one statement, then the next. C++ uses a compiler: it translates the whole program, then gives you a program you can run.

可以上网搜教程。搜完回到这一页,用通关对照判断自己做对没有。看到「一键安装包」或来路不明的下载,关掉。You may search for tutorials. Then come back and use the Pass check. Close any page that offers a mystery one-click pack.

输入命令前,把输入法换成英文。文件夹名不要加中文和空格。对照框里的字是结果,不要把它再打进命令行。Switch your keyboard to English before you type a command. Keep Chinese characters and spaces out of folder names. The Pass check is a result. Do not type it in.

1

把 Python 请到电脑上Invite Python onto the computer

Python 3.15.2,2026 年 10 月 25 日发布。页面上带 3.16 或 pre-release 的是测试版,这次不用。Use Python 3.15.2, released on 25 October 2026. A file marked 3.16 or pre-release is a test version. Leave it for today.

  1. 打开「命令提示符」:开始菜单输入 Open Command Prompt: in the Start menu, type cmd。.
  2. 建立文件夹。如果提示已经存在,继续下一关即可。Make the folders. If the computer says they already exist, go on to the next level.
    打进命令行Type this in
    mkdir C:\0478lab
    mkdir C:\0478lab\python
    cd /d C:\0478lab\python
  3. 安装文件用教师发的 Install the file from your teacher: python-3.15.2-amd64.exe(约 35.9 MB)。自己下载时打开 (about 35.9 MB). To download it yourself, open the Python 3.15.2 发布页Python 3.15.2 release page,点 and choose Windows installer (64-bit)。页面上的 Python install manager 这次不用。. Leave the Python install manager for today.
  4. 第一屏最下面勾选 On the first screen, tick Add python.exe to PATH。这句话的意思是:以后在任何文件夹都能叫到 Python。然后点 . This lets you call Python from any folder. Then click Install Now。.
  5. 关掉命令提示符,重新打开,输入:Close Command Prompt, open it again, and type:
    打进命令行Type this in
    py -3 --version
    py -3 -c "import sys; print(sys.executable)"

通关对照Pass check

下面是结果,不要输入。This is the result. Do not type it.

Python 3.15.2
C:\Users\YourName\AppData\Local\Programs\Python\Python315\python.exe

第一行要一字不差。第二行是解释器在硬盘上的位置,抄在纸上。文件夹名字可以和示例不同,只要以 python.exe 结尾。Line 1 must match exactly. Line 2 is where the interpreter lives. Copy it onto paper. The folders may differ from the example. The line should end with python.exe.

如果 If python 打开了应用商店: opens the Microsoft Store:到「设置 → 应用 → 高级应用设置 → 应用执行别名」,把 go to Settings → Apps → Advanced app settings → App execution aliases, and turn off python.exe 和 and python3.exe 关掉。后面继续用 . Keep using py -3。.
  1. 打开「终端」。Open Terminal.
  2. 安装教师发的 Install the file from your teacher: python-3.15.2-macos11.pkg(约 78.1 MB,给 macOS 10.15 及更新的系统。文件名里的 11 不是要求你的系统必须是 11)。自己下载时打开 (about 78.1 MB, for macOS 10.15 or newer. The 11 in the file name does not mean your Mac must be version 11). To download it yourself, open 同一个发布页the same release page,点 macOS installer。电脑原来自带的 Python 这次不用。 and choose the macOS installer. Leave the Python that came with the Mac.
  3. 关掉终端再打开:Close Terminal and open it again:
    打进终端Type this in
    python3 --version
    python3 -c "import sys; print(sys.executable)"

通关对照Pass check

下面是结果,不要输入。This is the result. Do not type it.

Python 3.15.2
/Library/Frameworks/Python.framework/Versions/3.15/bin/python3

第一行要一字不差。第二行抄在纸上。用官网安装包时,通常就是这一条。位置不同也可以,只要第一行是 Python 3.15.2。Line 1 must match exactly. Copy line 2 onto paper. The official installer usually prints this path. Another path is fine when line 1 is Python 3.15.2.

看到 Python 3.15.2,这一关就过了。解释器已经在你的电脑上。When you see Python 3.15.2, this level is done. The interpreter is now on your computer.
2

用最普通的记事本写四行Write four lines in a plain editor

先不要打开 VS Code。我们要看见:没有集成环境,程序也写得出来。Leave VS Code closed. You are about to see that a program can be written without an IDE.

打进命令行Type this in
cd /d C:\0478lab\python
notepad hello.py

问你要不要新建文件时,点「是」。把下面四行粘进去,按 If it asks to create a new file, choose Yes. Paste the four lines and press Ctrl+S。编码选 UTF-8。保存后回到命令行,输入 . Choose UTF-8. Then go back to Command Prompt and type type hello.py。.

打进终端Type this in
mkdir -p ~/0478lab/python
cd ~/0478lab/python
touch hello.py
open -e hello.py

如果窗口上有粗体、字号按钮,先选菜单「格式 → 制作纯文本」。引号要用直的 If you can see bold and font-size buttons, choose Format → Make Plain Text. Use straight quotes "。保存后回到终端,输入 . Save, return to Terminal, and type cat hello.py。.

存成 hello.pySave as hello.py
first = 20
second = 22
total = first + second
print(total)

这四行是在说:20 加 22,把结果记在 total 里,然后显示出来。These four lines say: add 20 and 22, remember the result as total, then show it.

通关对照Pass check

type hello.py 或 cat hello.py 应原样打出这四行。type hello.py or cat hello.py should print these four lines unchanged.

first = 20
second = 22
total = first + second
print(total)

文件名正好是 hello.py。如果是 hello.py.txt,先打开文件扩展名,删掉多余的 .txt,再对照一次。The file name is exactly hello.py. If it is hello.py.txt, show file extensions, remove the extra .txt, and check again.

四行对上了,程序已经写在文件里。下一关才让它算出 42。When the four lines match, the program is in the file. The next level makes it produce 42.
3

让命令行算出 42Let the command line work out 42

打进命令行Type this in
dir
py -3 hello.py

dir 里的名字必须正好是 hello.py。如果是 hello.py.txt,在资源管理器里打开「查看 → 显示 → 文件扩展名」,把多余的 .txt 删掉。In dir, the name must be exactly hello.py. If it is hello.py.txt, open View → Show → File name extensions in File Explorer and remove the extra .txt.

打进终端Type this in
ls
python3 hello.py

通关对照Pass check

程序自己打出的最后一行:The last line the program prints:

42

只有数字 42。没有引号,没有小数点,也没有别的字。Just the digits 42. No quotes, no decimal point, and no other words.

屏幕上的 42 是这四行算出来的。解释器读一行,做一行,做完才轮到下一行。That 42 came from the four lines. The interpreter reads one line, does it, then moves to the next line.
4

故意写错,看它在哪里停Make one mistake and see where it stops

这一关就是要看到报错。报错出现,说明你看懂了解释器。This level is meant to show an error. The error means you can see how the interpreter works.

打进命令行Type this in
notepad bad.py
打进终端Type this in
touch bad.py
open -e bad.py
存成 bad.pySave as bad.py
print("start")
print(first)
print("end")
打进命令行Type this in
py -3 bad.py
dir
打进终端Type this in
python3 bad.py
ls

通关对照Pass check

2026 年 11 月 19 日,Python 3.15.2 打出的是:On 19 November 2026, Python 3.15.2 printed:

start
Traceback (most recent call last):
  File "bad.py", line 2, in <module>
    print(first)
          ^^^^^
NameError: name 'first' is not defined. Did you mean: 'list'?

第一行是 start。NameError 那一行要有 first。end 不会出现。File 那一行可以带上文件夹路径。Did you mean 多一句也算过。dir 或 ls 里没有 exe,也没有可以单独拿走的程序。Line 1 is start. The NameError line includes first. end does not appear. The File line may include a folder path. An extra Did you mean line still passes. dir or ls shows no exe and no program you can take away on its own.

它做完第一句才发现第二句有错。这就是解释器:逐条做,遇错就停。It finished the first statement, then found the mistake in the second. That is an interpreter: one statement at a time, and it stops at an error.
5

请 VS Code 来到同一个窗口Bring VS Code into the same window

用 VS Code 1.147.0,2026 年 11 月 18 日的正式版。Windows 选 User Installer 的 x64,并勾选「添加到 PATH」。Mac 选 Universal 版,拖进「应用程序」。需要 macOS 12 或更新的系统。Use VS Code 1.147.0, the release from 18 November 2026. On Windows, choose the User Installer for x64 and tick Add to PATH. On a Mac, choose Universal and drag it to Applications. It needs macOS 12 or newer.

  1. 打开 VS Code,选择「文件 → 打开文件夹」,打开刚才的 Open VS Code, choose File → Open Folder, and open the python 文件夹。问你信不信任时,选信任。 folder. If it asks whether you trust the folder, choose trust.
  2. 左侧扩展里搜 Python,安装发布者是 In Extensions, search for Python and install the one published by Microsoft 的那个。别的「一键运行」扩展先不要装。. Leave other one-click run extensions for later.
  3. 按 Press Ctrl+Shift+P(Mac 用 (on a Mac, Command+Shift+P),输入 ), type Python: Select Interpreter,选纸上那条 3.15。列表里没有,就选 Enter interpreter path,把路径粘进去。, and choose the 3.15 path from your paper. If it is missing, choose Enter interpreter path and paste the path.
  4. 打开 Open hello.py,在字上面右键,选 , right-click the text, and choose Run Python File in Terminal。中文界面可能写成「在终端中运行 Python 文件」。. A Chinese menu may say「在终端中运行 Python 文件」.

通关对照Pass check

终端最后一行:The last line in the terminal:

42

最后一行上面的命令里,能看见 python 和 hello.py。数字仍是单独一行的 42。The command just above it shows python and hello.py. The number is still 42 on a line by itself.

又看到 42。看终端里的命令,VS Code 最后还是叫了同一个解释器。它把记事本、命令行和解释器放在了一起。There is 42 again. Look at the command: VS Code called the same interpreter. It put the editor, the command line, and the interpreter in one place.
6

试四个小帮手Try the four helpers

  1. 补全。Autocomplete.新的一行打 On a new line, type pr,列表里出现 . When the list shows print 就接受,然后把这行删掉。它只是帮你把单词补完。, accept it, then delete that line. It only finishes the word for you.
  2. 波浪线。The wavy line.把 Change print(total) 临时改成 to print(totl) 并保存。 and save. totl 下面会出现波浪线。看完改回去。 gets a wavy line. Then change it back.
  3. 整理书写。Tidy the writing.把缩进弄乱,按 Mess up the indent, then press Shift+Alt+F。Mac 按 . On a Mac, press Shift+Option+F。问你用哪个工具时,选 Python。它只把字排整齐。. If it asks which tool, choose Python. It only lines the words up.
  4. 暂停看变量。Pause and look at the variables.在 Click in the margin of the print(total) 这一行的行号左边点一下,出现红点。按 line so a red dot appears. Press F5。问你时选 Python Debugger,再选 Python File。让你安装 debugpy 时,安装。停下后看左边的变量。按 . Choose Python Debugger, then Python File. If it offers to install debugpy, install it. When it pauses, look at the variables on the left. Press Shift+F5 结束。 to finish.

通关对照Pass check

这一关看四个现象,不是一行数字。This level has four things to see. It is not one number.

你做的事What you do标准结果What you should see
pr列表里有 printThe list contains print
print(totl)totl 下面有波浪线A wavy line under totl
整理书写Tidy the writing缩进恢复整齐The indent is tidy again
红点暂停Pause on the red dotfirst = 20,second = 22,total = 42

看完波浪线后,把 totl 改回 total。四个都看见,这一关过了。After you see the wavy line, change totl back to total. All four sights mean this level is done.

四个都试过,第 6 关就过了。去告诉 C++ 组:我写错时,start 已经显示出来了。All four are done, so level 6 is done. Tell the C++ group: when I made a mistake, start had already appeared.
1

把编译器请到电脑上Invite the compiler onto the computer

编译器负责把整份程序翻译成一个可以运行的文件。The compiler translates the whole program into a file you can run.

主安装包是 MSYS2,版本 The main installer is MSYS2, version 20260927,2026 年 9 月 27 日发布。文件名 , released on 27 September 2026. The file is msys2-x86_64-20260927.exe,约 81 MB。官网写着 latest 的那个,在 2026 年 11 月 19 日也是这一份。, about 81 MB. On 19 November 2026, the file marked latest on the website was still this one.

  1. 运行安装包,安装位置保持 Run the installer and keep the location as C:\msys64。.
  2. 打开开始菜单里的 Open MSYS2 UCRT64。只叫 MSYS 的另一个图标这次不用。 from the Start menu. Leave the icon that only says MSYS.
  3. 先更新。窗口若自己关掉,重新打开 UCRT64,再执行一次,直到它不再要求关闭。Update first. If the window closes itself, open UCRT64 again and run the command once more, until it stops asking to close.
    打进 UCRT64Type this in UCRT64
    pacman -Syu
  4. 安装编译器和调试器。出现名单按 Enter,问你是否继续时输入 Install the compiler and the debugger. Press Enter for the list. When it asks to continue, type Y。.
    打进 UCRT64Type this in UCRT64
    pacman -S --needed mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-gdb
  5. 还在这个窗口里确认。只要出版本号就行。官方示例里常见 GCC 16.2.0,数字更高也可以。Still in this window, check the versions. Any version number is enough. The official example often shows GCC 16.2.0. A higher number is fine.
    打进 UCRT64Type this in UCRT64
    g++ --version
    gdb --version
  6. 让普通命令行也找得到它。开始菜单搜「编辑账户的环境变量」,在用户变量的 Path 里新建一行 Let ordinary Command Prompt find it too. Search the Start menu for “Edit environment variables for your account”. Under the user variable Path, add C:\msys64\ucrt64\bin。Path 就是电脑找程序时会查看的文件夹名单。. Path is the list of folders the computer searches when you ask for a program.
  7. 关掉旧窗口,新开「命令提示符」:Close the old window and open a new Command Prompt:
    打进命令行Type this in
    g++ --version
    gdb --version
    where g++
    where gdb

通关对照Pass check

形状像这样。括号里的字可以不同。The shape looks like this. The words in brackets may differ.

g++.exe (...) 16.2.0
GNU gdb (GDB) ...
C:\msys64\ucrt64\bin\g++.exe
C:\msys64\ucrt64\bin\gdb.exe

g++ 和 gdb 都要有版本号。16.2.0 或更高都算过。where 印出的两条路径都在 ucrt64\bin 里,把它们抄在纸上。Both g++ and gdb show a version number. 16.2.0 or higher passes. The two paths from where are inside ucrt64\bin. Copy them onto paper.

学校电脑不允许安装软件时If the school computer does not allow this install

用教师发的 w64devkit Use w64devkit 2.11.0,2026 年 10 月 29 日发布。文件名是 from your teacher, released on 29 October 2026. The file is w64devkit-x64-2.11.0.7z.exe,约 65.6 MB。文件名里带 x86 的那一个这次不用。, about 65.6 MB. Leave the file whose name contains x86.

  1. 解压后找到 After it unpacks, find g++.exe。它所在的 . The bin 文件夹就是要加入 Path 的地方。 folder that contains it is the folder you add to Path.
  2. 双击 Double-click w64devkit.exe,输入 and type g++ --version 和 and gdb --version。.
  3. 把这个 Add this bin 加进用户变量 Path。这一步通常不需要管理员。 to the user Path variable. This step usually needs no administrator.
  4. 新开命令提示符,再执行 Open a new Command Prompt and run where g++。后面的命令都在命令提示符里打。纸上的路径代替后文里的 . Type the later commands in Command Prompt. Use the path from your paper in place of C:\msys64\ucrt64\bin。 later on.

这条备用路的通关对照Pass check for this spare route

g++.exe (...) ...
GNU gdb (GDB) ...
C:\path\on\your\paper\g++.exe
C:\path\on\your\paper\gdb.exe

两条 where 路径都落在你找到 g++.exe 的那个 bin 里,并且两条 version 都有数字。Both where paths are in the bin folder that contains g++.exe, and both version commands show a number.

Mac 不用再下一个很大的安装包。系统会弹出「命令行工具」,里面就有编译器。A Mac does not need another large installer. The system offers Command Line Tools, and the compiler is inside them.

打进终端Type this in
xcode-select --install

弹出窗口后选择安装,等它结束。如果它说已经装过,直接继续。装完新开一个终端:Choose Install in the window and wait. If it says the tools are already there, continue. Then open a new Terminal:

打进终端Type this in
g++ --version
clang++ --version
lldb --version
which clang++

通关对照Pass check

2026 年 11 月 19 日,教师机的前两行是:On 19 November 2026, the teacher’s computer began with:

Apple clang version 21.0.0 (clang-2100.4.1.1)
Target: arm64-apple-darwin25.6.0
/usr/bin/clang++

g++、clang++、lldb 三条都要有版本号。which 印出 /usr/bin/clang++,把这一条抄下来。括号里的小版本、Target 里的系统号可以不同。Intel 芯片的 Target 里会出现 x86_64,也算过。这里的 g++ 会去调用 Apple 的编译器,做的事仍然是:整份翻译,生成一个能运行的程序。g++, clang++, and lldb each show a version. which prints /usr/bin/clang++. Copy that line. The small build number in brackets, and the system number in Target, may differ. An Intel Mac may show x86_64 in Target. That still passes. Here g++ calls Apple’s compiler. It still translates the whole program and makes a file you can run.

版本号出现了,编译器就请到了。A version number means the compiler has arrived.
2

用记事本写下同一道加法Write the same addition in a plain editor

打进命令行Type this in
mkdir C:\0478lab
mkdir C:\0478lab\cpp
cd /d C:\0478lab\cpp
notepad hello.cpp

新建文件后粘贴下面的程序。另存为时编码选 Create the file and paste the program. When you save, choose ANSI,按 , then press Ctrl+S。回到命令行,输入 . Back in Command Prompt, type type hello.cpp。.

打进终端Type this in
mkdir -p ~/0478lab/cpp
cd ~/0478lab/cpp
touch hello.cpp
open -e hello.cpp

有格式按钮时,先选「格式 → 制作纯文本」。引号用直的。保存后输入 If you can see formatting buttons, choose Format → Make Plain Text. Use straight quotes. After saving, type cat hello.cpp。.

存成 hello.cppSave as hello.cpp
#include <iostream>
int main() {
    int first = 20;
    int second = 22;
    int total = first + second;
    std::cout << total << std::endl;
    return 0;
}

意思和 Python 那四行一样:20 加 22,把 42 显示出来。写法更严,所以它仍然是高级语言。It means the same thing as the four Python lines: add 20 and 22, then show 42. The writing has stricter rules, and it is still a high-level language.

通关对照Pass check

type hello.cpp 或 cat hello.cpp 应打出和上面一样的程序。type hello.cpp or cat hello.cpp should print the same program as the box above.

#include <iostream>
int main() {
    int first = 20;
    int second = 22;
    int total = first + second;
    std::cout << total << std::endl;
    return 0;
}

文件名正好是 hello.cpp。尖括号和两个小于号都要在。The file name is exactly hello.cpp. The angle brackets and the two less-than signs are all there.

源程序对上了。下一关才请编译器把它整份翻译出来。The source program matches. The next level asks the compiler to translate all of it.
3

翻译出成品,再运行Translate it, then run the result

打进命令行Type this in
dir
g++ -o hello.exe hello.cpp
dir
hello.exe

如果报错里有 If the error contains stray 和 and \357,回到记事本,用 ANSI 编码另存为,覆盖原文件,再编译一次。, go back to Notepad, save again as ANSI over the old file, and compile once more.

通关对照Pass check

第二次名单里多出 hello.exe。运行后的最后一行:The second list has a new hello.exe. After you run it, the last line is:

42

dir 还会显示日期和大小,那些可以不同。新出现的文件名必须是 hello.exe,最后一行必须是 42。dir also shows dates and sizes. Those may differ. The new file name is hello.exe, and the last line is 42.

打进终端Type this in
ls
g++ -o hello hello.cpp
ls -l
./hello

通关对照Pass check

第二次名单里多出 hello,没有 .exe。运行后的最后一行:The second list has a new hello, with no .exe. After you run it, the last line is:

42

ls -l 还会显示权限和大小。那些可以不同。新文件名必须是 hello,最后一行必须是 42。ls -l also shows permissions and size. Those may differ. The new file name is hello, and the last line is 42.

42 又出现了。这一次翻译是整份做完的,文件夹里留下了可以运行的成品。42 is back. This time the translation finished the whole program, and a runnable result stayed in the folder.
4

故意写错,看它交不交新程序Make one mistake and see whether a new program appears

打进命令行Type this in
notepad bad.cpp
打进终端Type this in
touch bad.cpp
open -e bad.cpp
存成 bad.cppSave as bad.cpp
#include <iostream>
int main() {
    std::cout << "start" << std::endl;
    std::cout << first << std::endl;
    std::cout << "end" << std::endl;
    return 0;
}
打进命令行Type this in
g++ -o hello2.exe bad.cpp
dir hello2.exe
hello.exe

通关对照Pass check

报告里要有这一句。前后可能还有别的行。The report contains this line. Other lines may appear around it.

error: 'first' was not declared in this scope

然后原来的 hello.exe 再打出:Then the old hello.exe prints:

42

dir hello2.exe 找不到文件。屏幕上没有 start。报告里有 first。旧程序的最后一行仍是 42。这三件一起算过。dir hello2.exe cannot find the file. start does not appear. The report contains first. The old program still ends with 42. All three together are a pass.

打进终端Type this in
g++ -o hello2 bad.cpp
ls hello2
./hello

通关对照Pass check

2026 年 11 月 19 日,教师机打出的报告是:On 19 November 2026, the teacher’s computer printed:

bad.cpp:4:18: error: use of undeclared identifier 'first'
    4 |     std::cout << first << std::endl;
      |                  ^~~~~
1 error generated.

原来的 ./hello 再打出:Then the old ./hello prints:

42

ls hello2 会说没有这个文件。报告里有 first 和 undeclared identifier。屏幕上没有 start。旧程序的最后一行仍是 42。ls hello2 says there is no such file. The report contains first and undeclared identifier. start does not appear. The old program still ends with 42.

编译器发现整份程序有错,就一份新程序都不交。旧的成品还在,因为它上次已经翻译成功了。The compiler found an error in the whole program, so it handed over no new program. The old result is still there, because that translation had already succeeded.
5

只带走成品Take only the finished program

打进命令行Type this in
mkdir ship
copy hello.exe ship\
cd ship
hello.exe
dir

通关对照Pass check

运行打出 42。随后的名单里有 hello.exe。Running it prints 42. The list afterwards contains hello.exe.

42

ship 里没有 hello.cpp,也没有再调用 g++。名单里的日期和大小可以不同。ship has no hello.cpp, and you did not call g++ again. Dates and sizes in the list may differ.

打进终端Type this in
mkdir -p ship
cp hello ship/
cd ship
./hello
ls

通关对照Pass check

运行打出 42。随后的名单里有 hello。Running it prints 42. The list afterwards contains hello.

42

ship 里没有 hello.cpp,也没有再调用 g++。ship has no hello.cpp, and you did not call g++ again.

ship 里没有源文件,42 还在。所以发给很多用户时,可以只给这个成品。ship has no source file, and 42 is still there. When a finished program goes to many users, this result is enough to send.
6

让 VS Code 把这些事放在一起Let VS Code hold these jobs together

安装 VS Code Install VS Code 1.147.0(2026 年 11 月 18 日)。Windows 用 User Installer 的 x64,Mac 用 Universal。扩展只装 Microsoft 的 (18 November 2026). Windows uses the User Installer for x64. Mac uses Universal. Install only Microsoft’s C/C++。打开 extension. Open the cpp 文件夹并信任它。先回到上一级文件夹,再打开 cpp。如果现在还在 ship 里面,成品会生成到错误的地方。 folder and trust it. Move up one folder first, then open cpp. If you are still inside ship, the result will be created in the wrong place.

  1. 打开 Open hello.cpp。按 . Press Ctrl+Shift+P(Mac 为 (on a Mac, Command+Shift+P),输入 ) and type Edit Configurations (UI)。这是一张设置表,照着填。. This opens a settings form. Fill it in as shown.
填哪里Where to typeWindows,MSYS2Windows, MSYS2Windows,w64devkitWindows, w64devkitMac
Compiler path
编译器在哪where the compiler is
C:/msys64/ucrt64/bin/g++.exe纸上的 g++.exethe g++.exe from your paper/usr/bin/clang++
IntelliSense mode
哪一种编译器which compiler
windows-gcc-x64windows-gcc-x64先在终端打 In Terminal, type uname -m。是 arm64 就选 macos-clang-arm64,是 x86_64 就选 macos-clang-x64. Choose macos-clang-arm64 for arm64, or macos-clang-x64 for x86_64
C++ standardc++17c++17c++17
  1. 菜单选「终端 → 配置默认生成任务」。Windows 选带 g++.exe 的那一项,Mac 选 clang++ 或 g++。名单是空的,就打开本关最底下的备用配置。Choose Terminal → Configure Default Build Task. On Windows, choose the item with g++.exe. On a Mac, choose clang++ or g++. If the list is empty, open the spare settings at the bottom of this level.
  2. 按 Press Ctrl+Shift+B(Mac 为 (on a Mac, Command+Shift+B)。最后看到退出代码 0,就是翻译成功。英文界面写成 exit code: 0。). Exit code 0 means the translation succeeded. A Chinese window may say 退出代码 0.

然后在 VS Code 的终端里运行。这里要加 Then run it in the VS Code terminal. Add .\,因为这个终端常常是 PowerShell。 here, because this terminal is often PowerShell.

打进 VS Code 终端Type this in the VS Code terminal
.\hello.exe
打进 VS Code 终端Type this in the VS Code terminal
./hello
  1. 新的一行打 On a new line, type std::co,出现 . When cout 就接受,再删掉这行。这是自动完成。没有列表时,回到设置表看看编译器路径。 appears, accept it, then delete that line. This is autocomplete. If there is no list, check the compiler path on the settings form.
  2. 把 Change total 临时改成 to totl,再按生成快捷键。这次必须失败,报告里有 , then press the build shortcut. This one must fail, and the report contains totl。改回去,再次生成成功。. Change it back, and build successfully again.
  3. 左边「运行和调试」里新建 In Run and Debug on the left, create launch.json,选 C++ (GDB/LLDB)。Windows 选 g++ 那一项,Mac 选 clang++ 那一项。对不上就用备用配置。 and choose C++ (GDB/LLDB). On Windows choose the g++ item. On a Mac choose the clang++ item. If nothing matches, use the spare settings.
  4. 在显示 total 的那一行左边点出红点,按 Click a red dot in the margin of the line that shows total, then press F5。能看见 first 是 20,second 是 22,total 是 42。按 . You can see first is 20, second is 22, and total is 42. Press Shift+F5 结束。 to finish.

通关对照Pass check

你做的事What you do标准结果What you should see
生成 hello.cppBuild hello.cpp退出代码 0,英文界面是 exit code: 0exit code: 0
运行成品Run the result
42
std::co列表里有 coutThe list contains cout
totl生成失败,报告里有 totlThe build fails and the report contains totl
红点暂停Pause on the red dotfirst = 20,second = 22,total = 42

把 totl 改回 total 之后,再生成一次,又是退出代码 0。After you change totl back to total, build once more and get exit code 0 again.

生成按钮最后还是调用了第 1 关的编译器。去告诉 Python 组:我写错时,start 根本没出现,也没有新程序。The build button still called the compiler from level 1. Tell the Python group: when I made a mistake, start never appeared, and there was no new program.

菜单里没有编译器时才看Look here only when the menu has no compiler

工作区里没有 If the workspace has no .vscode 文件夹就新建一个。两段都整份替换。里面的任务名字必须彼此一样。 folder, create one. Replace each file completely. The task names must match each other.

Windows 的 tasks.jsontasks.json for Windows
.vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",
      "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
      "args": ["-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
      "options": { "cwd": "${fileDirname}" },
      "problemMatcher": ["$gcc"],
      "group": { "kind": "build", "isDefault": true }
    }
  ]
}
Windows 的 launch.jsonlaunch.json for Windows
.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++: g++.exe build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
      "setupCommands": [
        { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }
      ],
      "preLaunchTask": "C/C++: g++.exe build active file"
    }
  ]
}

用 w64devkit 的同学,把两处 If you use w64devkit, replace both copies of C:\\msys64\\ucrt64\\bin 换成纸上的 bin 路径,反斜杠写成 with the bin path from your paper. Write each backslash as \\。.

Mac 的 tasks.jsontasks.json for Mac
.vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": ["-std=c++17", "-fcolor-diagnostics", "-fansi-escape-codes", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": { "cwd": "${fileDirname}" },
      "problemMatcher": ["$gcc"],
      "group": { "kind": "build", "isDefault": true }
    }
  ]
}
Mac 的 launch.jsonlaunch.json for Mac
.vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++: clang++ build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "lldb",
      "preLaunchTask": "C/C++: clang++ build active file"
    }
  ]
}

两组碰头The two groups meet

用自己的屏幕,把下面三句说给另一组听。Use your own screen and say these three things to the other group.

通关对照Pass check

Python 组说:写错时,start 已经显示了,然后停在第 2 行的 NameError。文件夹里没有单独的程序。The Python group says: when the program was wrong, start had already appeared, then it stopped at the NameError on line 2. The folder had no separate program.

C++ 组说:写错时,start 没有显示,也没有新程序。ship 里只有成品,最后一行仍是 42。The C++ group says: when the program was wrong, start did not appear, and there was no new program. ship had only the finished program, and its last line was still 42.

一起说:两种都是高级语言。一个用解释器,一个用编译器。Together: both are high-level languages. One uses an interpreter. One uses a compiler.

隐藏关卡还锁着The hidden level is still locked

要等 Python 组和 C++ 组都通关。老师把口令写在黑板上之后,再输入。口令是四个汉字,两种语言都输入这一句。Wait until both the Python group and the C++ group have finished. Then type the password the teacher writes on the board. It is four Chinese characters. Type those characters in either language.