如何利用Python开发一个简单的猜数字游戏


Posted in Python onSeptember 22, 2019

前言

本文介绍如何使用Python制作一个简单的猜数字游戏。

游戏规则

玩家将猜测一个数字。如果猜测是正确的,玩家赢。如果不正确,程序会提示玩家所猜的数字与实际数字相比是“大(high)”还是“小(low)”,如此往复直到玩家猜对数字。

准备好Python3

首先,需要在计算机上安装Python。可以从Python官网下载并安装。本教程需要使用最新版的Python 3(版本3.x.x)。

确保选中将Python添加到PATH变量的框。如果不这样做,将很难运行该程序。

现在,在设备上打开文本/代码编辑器。就个人而言,我偏好使用Brackets。 Windows上预装了Notepad, Mac OS包含TextEdit,而Linux用户可以使用Vim。

打开文本编辑器后,保存新文件。我将它命名为main.py,但你可以随意命名,只要它以.py结尾即可。

如何利用Python开发一个简单的猜数字游戏

编码

本教程的说明将作为注释包含在代码中。 在Python中,注释以#开头并一直持续到行结束。

from keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D
# First, we need to import the 'random' module.
# This module contains the functionality we need to be able to randomly 
select the winning number.

import random

# Now, we need to select a random number.
# This line will set the variable 'correct' to be equal to a random
 integer between 1 and 10.

correct = random.randint(1, 10)
# Let's get the user's first guess using the 'input' function.

guess = input("Enter your guess: ")

# Right now, the user's input is formatted as a string.
# We can format it as an integer using the 'int' function.

guess = int(guess)

# Let's start a loop that will continue until the user has guessed
 correctly.
# We can use the '!=' operator to mean 'not equal'.

while guess != correct:
# Everything in this loop will repeat until the user has guessed
 correctly.
# Let's start by giving the user feedback on their guess. We can do
 this using the 'if' statement.

# This statement will check if a comparison is true.
# If it is, the code inside the 'if' statement will run.

if guess > correct:

# This code will run if the user guessed too high.
# We can show a message to the user using the 'print' function.

print("You've guessed too high. Try guessing lower.")

else:

# The 'else' statement adds on to an 'if' statement.
# It will run if the condition of the 'if' statement is false.

# In this case, it will run if the user guessed too low, so we can give
 them feedback.

print("You've guessed too low. Try guessing higher.")

# Now we need to let the user guess again.
# Notice how I am combining the two lines of guessing code to make just 
one line.

guess = int(input("Enter your guess: "))

# If a user's guess is still incorrect, the code in the 'while' loop
 will be repeated
.# If they've reached this point in the code, it means they guessed
 correctly, so let's say that.

print("Congratulations! You've guessed correctly.")

此外,可以随意更改程序中的任何内容。

例如,可以将正确的数字设置为1到100而不是1到10,可以更改程序在print()函数中所说的内容。你的代码想怎么写都可以。

如何利用Python开发一个简单的猜数字游戏

运行程序

根据你的操作系统,打开命令提示符(Windows / Linux)或终端(Mac)。 按顺序尝试以下每个命令。 如果正确安装Python,其中至少有一个应该可以运行。

python C:/Users/username/Desktop/main.py

py C:/Users/username/Desktop/main.py

python3 C:/Users/username/Desktop/main.py

确保将C:/Users/username/Desktop/main.py替换为Python文件的完整路径。程序运行后,可测试一下,玩几次! 完成操作后,按向上箭头键复制最后一个命令,然后按Enter即可再次运行。以下是没有任何注释的代码版本:

import random

correct = random.randint(1, 10)

guess = input("Enter your guess: ")
guess = int(guess)

while guess != correct:
if guess > correct:
print("You've guessed too high. Try guessing lower.")
else:
print("You've guessed too low. Try guessing higher.")

guess = int(input("Enter your guess: "))
print("Congratulations! You've guessed correctly.")

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python制作最美应用的爬虫
Oct 28 Python
Python遍历目录中的所有文件的方法
Jul 08 Python
基于Linux系统中python matplotlib画图的中文显示问题的解决方法
Jun 15 Python
Python文件操作基本流程代码实例
Dec 11 Python
pyhanlp安装介绍和简单应用
Feb 22 Python
浅谈Python中eval的强大与危害
Mar 13 Python
python 绘制拟合曲线并加指定点标识的实现
Jul 10 Python
解决django中ModelForm多表单组合的问题
Jul 18 Python
Python绘制三角函数图(sin\cos\tan)并标注特定范围的例子
Dec 04 Python
Python3.6安装卸载、执行命令、执行py文件的方法详解
Feb 20 Python
python+requests接口自动化框架的实现
Aug 31 Python
Python Tkinter实例——模拟掷骰子
Oct 24 Python
Python中关于浮点数的冷知识
Sep 22 #Python
Python安装及Pycharm安装使用教程图解
Sep 20 #Python
Python实现语音识别和语音合成功能
Sep 20 #Python
使用python将最新的测试报告以附件的形式发到指定邮箱
Sep 20 #Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
Sep 20 #Python
Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析
Sep 20 #Python
Python 类属性与实例属性,类对象与实例对象用法分析
Sep 20 #Python
You might like
PHP 高手之路(三)
2006/10/09 PHP
PHP处理JSON字符串key缺少双引号的解决方法
2014/09/16 PHP
PHP通过调用新浪API生成t.cn格式短网址链接的方法详解
2019/02/20 PHP
浅析PHP7的多进程及实例源码
2019/04/14 PHP
Javascript表格翻页效果实现思路及代码
2013/08/23 Javascript
jquery实现tr元素的上下移动示例代码
2013/12/20 Javascript
jquery 表单验证之通过 class验证表单不为空
2015/11/02 Javascript
javascript设计简单的秒表计时器
2020/09/05 Javascript
基于vue2.0+vuex的日期选择组件功能实现
2017/03/13 Javascript
Angular组件化管理实现方法分析
2017/03/17 Javascript
微信小程序显示倒计时功能示例【测试可用】
2018/12/03 Javascript
解决vue初始化项目时,一直卡在Project description上的问题
2019/10/31 Javascript
Angular之jwt令牌身份验证的实现
2020/02/14 Javascript
微信小程序scroll-view点击项自动居中效果的实现
2020/03/25 Javascript
element-ui和vue表单(对话框)验证提示语(残留)清除操作
2020/09/11 Javascript
JS如何生成动态列表
2020/09/22 Javascript
JavaScript实现打字游戏
2021/02/19 Javascript
[05:59]带你看看DPC的台前幕后
2021/03/11 DOTA
python解析发往本机的数据包示例 (解析数据包)
2014/01/16 Python
在Python中使用AOP实现Redis缓存示例
2017/07/11 Python
Python 判断 有向图 是否有环的实例讲解
2018/02/01 Python
浅谈Tensorflow模型的保存与恢复加载
2018/04/26 Python
python实现图片筛选程序
2018/10/24 Python
python 插入日期数据到Oracle实例
2020/03/02 Python
scrapy框架携带cookie访问淘宝购物车功能的实现代码
2020/07/07 Python
css3实现背景动态渐变效果
2019/12/10 HTML / CSS
String、StringBuffer、StringBuilder有区别
2015/09/18 面试题
18岁生日感言
2014/01/12 职场文书
《棉鞋里的阳光》教学反思
2014/04/24 职场文书
目标责任书格式范文
2015/05/11 职场文书
学子宴致辞大全
2015/07/27 职场文书
PyTorch 如何自动计算梯度
2021/05/23 Python
MySQL中CURRENT_TIMESTAMP的使用方式
2021/11/27 MySQL
JavaScript中MutationObServer监听DOM元素详情
2021/11/27 Javascript
Java 深入探究讲解简单工厂模式
2022/04/07 Java/Android
使用scrapy实现增量式爬取方式
2022/06/21 Python