畅享聊:Python的人机对战小游戏《石头,剪刀,布》

畅享聊:Python的人机对战小游戏《石头,剪刀,布》

首页休闲益智石头剪刀布大乱斗更新时间:2024-04-11

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard新学苑。今天小编为你带来

畅享聊:Python的人机对战小游戏《石头,剪刀,布》,

欢迎您的访问!

Share interests, spread happiness, increase knowledge, and leave beautiful!

Dear you, this is LearningYard Academy.

Today, the editor brings you

Enjoy chatting: Python's human-computer game Stone, Scissors, Paper,

welcome your visit!

Python是近几年非常火的一种编程语言,它的火爆不是偶然现象,近些年来,随着大数据、人工智能的兴起,Python的优势越发明显。Python的代码比较简洁,能够兼容众多的平台且拥有非常丰富的库,同时它还拥有相当强大的数据处理能力。那么今天就让小编带着大家一起看看用Python的人机对战小游戏——《石头,剪刀,布》

Python is a very popular programming language in recent years. Its popularity is not an accidental phenomenon. In recent years, with the rise of big data and artificial intelligence, Python's advantages are more obvious. Python code is simple, compatible with many platforms, and has a very rich library. At the same time, it also has a very powerful data processing capability. So today, let's let Xiao Bian lead us to watch the Python human-computer game Stone, Scissors, and Cloth

首先我们需要一张流程图。通过流程图,我们能够很清晰地看到我们程序的各个步骤及其之间的逻辑关系。流程图就像是说明指南,防止在写代码的过程中迷失方向,而且当我们的代码在最后出现bug跑不起来时,我们能够与流程图一一对照,来找到bug。这是小编画的本次的流程图。

First, we need a flow chart. Through the flow chart, we can clearly see the steps of our program and the logical relationship between them. The flow chart is like an instruction guide to avoid getting lost in the process of writing code, and when our code fails to run in the end, we can compare it with the flow chart one by one to find the bugs. This is the flow chart drawn by Xiao.

1

错 误 示 图

有了流程图,接下来就要着手于代码的编写。由于我们想要电脑猜拳是随机的出拳,所以我们在开头需要引入模块random,random(1,3)表示在1到3(包括3)内随机取数。想要引入这个模块,我们需要用到import函数。假如不用import函数引入就会出现上图这种情况,系统报错,告诉我们random在之前没有出现过,就是电脑没看懂random从何而来。这也很好地向我们展示了引入函数的作用与重要性。并且为了使游戏更加完整,我们需要记录游戏的分数。于是在开头要敲上玩家与电脑的初始分数都为0分。之后就按照流程图一步一步敲代码,代码如下图。

With the flow chart, the next step is to start writing the code. Since we want computer guessing to be random, we need to introduce the module random at the beginning. Random (1,3) means random access from 1 to 3 (including 3). To introduce this module, we need to use the import function. If the import function is not used, the situation shown in the figure above will occur. The system reports an error and tells us that random has not appeared before, but the computer does not understand where random comes from. This also shows us the role and importance of introducing functions. And in order to make the game more complete, we need to record the score of the game. So at the beginning, the initial score of both players and computers is 0. Then click the code step by step according to the flow chart, as shown in the following figure.

其实小编在编写这个代码时也出现了许许多多的问题,单词拼写错误,缩进有误,符号遗漏等等。一旦有点小错误就会导致代码跑不起来,但并不代表代码能运行就没有问题了,它的运行方向可能和我们所设计的流程有出入。最好的检查方法当然就是运行一下,亲自测测。每写完一个板块都去测测,个人认为这会提高正确性与效率!

In fact, when Xiao Bian was writing this code, there were many problems, such as spelling mistakes, indentation errors, symbol omissions, etc. Once there is a small error, the code will not run, but it does not mean that the code can run. Its running direction may be different from the process we designed. The best way to check is, of course, to run it and test it yourself. I think it will improve the accuracy and efficiency!

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天,

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article,

Welcome to leave us a message,

Let us meet tomorrow,

I wish you a happy day today!

本文由learningyard新学苑原创,如有侵权,请联系我们。

参考内容:B站Python视频教学内容

翻译:百度翻译

文字&排版|calm

审核|闫庆红

,
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved