我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

在 Python 中构建一个简单的测验游戏

根据TIOBE 指数, Python 是全球最流行的编程语言,其受欢迎的原因显而易见:Python 程序简洁清爽、易于阅读,括号数量远少于其他一些语言,因而无需费力理清嵌套结构。《第 163 期》Raspberry Pi Official Magazine刊登了面向初学者的 Python 项目,专为刚接触树莓派的新手设计。

作者肖恩·麦克马纳斯坚信,学习编程的最佳方式就是动手实践。他撰写了本教程,手把手教你构建一个简单的测验程序,讲解节奏平缓、适合入门者。请将本文分享给那些对编程跃跃欲试却不知从何起步的朋友。

01. 打开 Thonny

树莓派操作系统(Raspberry Pi OS)预装了 Thonny,可用于编写和运行 Python 程序。你可在树莓派桌面菜单的“编程”类别下找到它。

每位程序员在编写下一个精彩脚本前都会看到的画面

02. 输入你的第一个程序

Thonny 包含两个面板:底部是“Shell(交互式命令行)”。在此输入指令,系统会立即执行;我们将重点关注顶部面板——你在此处编写程序,即一组可保存、复用并轻松编辑的指令集。请键入以下简短程序——你能预测它的运行效果吗?

name = input("你叫什么名字?")  

print("你好", name)

在 Thonny 中,你会看到"What is your name?" and "Hello" are green, which means they’re chunks of text known as ‘strings’. Strings have quotation marks around them.

03. 运行你的程序

单击绿色的“运行”按钮以启动程序。若运行失败,请仔细检查代码,尤其是标点符号。

第一行使用了 Python 的input() function, which tells the user what it wants and then waits for them to type it in. The user’s input goes into a variable called name. A variable is like a box for storing information. A program can change (or vary) the information in a variable.

第二行中,print() function outputs information in the Shell. Inside the brackets, we put the information we want to display: the string "Hello" and the variable that contains the user’s name. We separate them with a comma. Can you modify the program to say “[name] is awesome”?

在顶部面板编写程序,并在 Shell 中查看输出结果

04. 添加一道测验题目

我们来将该程序扩展为一个“猜年份”测验游戏,如图所示。simple_quiz.py.

游戏从第 5 行开始。我们提出测验问题,并将玩家的答案存入名为guess. The input() function creates string variables, so line 6 converts guess into an integer (whole number) so that we can do number comparisons. The # symbol marks a comment, added to help people understand the code. The computer ignores comments.

第 7 行与第 8 行构成一组语句。第 7 行检查变量中的数值是否guess variable is equal to 1837, the correct answer. Python uses two equals signs to check for things that are the same. (In lines 1, 5, and 6, you saw one equals sign, which is used to mean “make this variable equal to” something.) The instruction in line 8 is only carried out if the player’s guess is 1837.

你可以看出第8行与第7行属于同一代码块,因为它的缩进为四个空格。Python使用缩进表示哪些语句属于同一逻辑组,而无需额外的标点符号(例如花括号)。如果缩进错误,程序将无法运行。输入程序时,若记得在第7行末尾加上冒号“:”,Thonny会自动为下一行添加缩进。当不再需要缩进时,按“DELETE”键即可。

同理,第9行和第10行用于检查玩家猜测的年份是否小于1837,并据此给出相应回答;第11行和第12行则处理大于(或晚于)1837的猜测。

通过输入较早、较晚及正确的年份来测试你的程序是否正常工作。恭喜你!你已成功构建了自己的第一个Python游戏!

这个简单的问答游戏会向用户提问一道历史题,要求其猜出正确年份;
下载完整代码

05. 理解列表

你可以通过复制第5至12行、粘贴到程序末尾,并为每个问题修改相应代码来添加更多题目。但还有更好的方法。

列表(list)是Python中一种用于存储信息的数据结构,它能帮你编写更简短、更清晰且更高级的程序。单击Thonny的“新建(New)”按钮以打开新标签页,然后输入以下代码:tea_menu.py listing.

在本程序中,第3行创建了一个名为tea_menu. It contains four strings. Each list item is separated by a comma, and the whole list is enclosed within square brackets.

第4行和第5行创建了一个for loop. A loop is a portion of code that repeats. Line 4 sets it up so that each time the loop starts, the program takes the next item from the tea_menu list and puts it into a variable called flavour. Line 5 is indented to show it belongs to the loop — it outputs the tea flavour. Try adding more flavours to the list.

我们还可以通过列表中元素的位置(即索引)来访问特定项。索引从零开始编号,因此第7行和第8行展示了如何获取前两种口味。该len() function reveals how long a list is, as shown in line 9.

关于列表还有很多知识可学,但今天我们只需掌握以上内容即可。

对\n in the strings we’re printing? It adds a blank line to tidy up the output.


此菜单由一个茶叶口味列表动态生成;下载完整代码

06. 创建基于列表的问答游戏

让我们把此前所学内容与几个新概念结合起来:我们将制作一款包含多道题目的问答游戏——玩家持续猜测直至答对,然后程序自动进入下一题。参见longer_quiz.py. 

第一个新概念:列表可以包含其他列表。本程序创建了一个名为question_and_answer_list (lines 3 to 7). Each item in it is a list that contains a single question (a string) and its answer (a number). 

第9行设置了一个循环,遍历大型题目与答案列表;它依次提取下一对题目与答案,并存入名为question and answer.

第二个新概念是:可以在一个循环内部嵌套另一个循环,称为“嵌套循环”。本程序引入了while loop starting at line 12 and ending at line 21. This type of loop repeats a chunk of code while something is true. In this case, we repeat as long as the player’s guess is not equal to (!=) the answer. We indent the lines below by another four spaces to show which lines belong to this loop.

使用较早、较晚及正确答案测试问答游戏;下载完整代码

因此,我们有一个外层循环遍历所有题目,还有一个内层循环接收玩家的猜测、告知其猜测结果是偏早、偏晚还是正确,并持续循环直至答对为止。此时,程序跳回第9行,提取下一对题目与答案。

第1行创建了一个变量,用于记录玩家的猜测次数;该变量在第15行递增。最后,程序向玩家显示其得分。第13行中,问题字符串末尾添加了一个空格,以优化显示效果;否则,玩家的输入将紧贴问题文字显示。

在学习Python过程中,自定义程序是良好实践。请尝试为问答游戏添加自己的题目和提示语。

更多树莓派项目,请参阅Raspberry Pi Official Magazine

本文刊登于第163期的Raspberry Pi Official Magazine,您可以在线访问您还可以订阅订阅我们杂志的印刷版。我们不仅支持全球配送,而且凡订阅六个月或十二个月印刷版的用户,还将获赠一台免费的树莓派 Pico 2 W!

您可以在Raspberry Pi Official Magazine上找到脸书, X, 主题讨论, 领英,以及Mastodon您也可以通过电子邮件联系团队:magazine@raspberrypi.com

2条评论

David G. avatar

我会把这本书标记为已收藏吗?

Sufiyan avatar

这是一篇对 Python 初学者非常有帮助的教程。这款问答游戏简单有趣,是练习基础编程概念的绝佳方式。感谢分享!

评论已关闭