如何利用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装饰器由浅入深
Dec 09 Python
浅析Python中return和finally共同挖的坑
Aug 18 Python
关于python的list相关知识(推荐)
Aug 30 Python
python实现音乐下载的统计
Jun 20 Python
python matlibplot绘制3D图形
Jul 02 Python
django 中的聚合函数,分组函数,F 查询,Q查询
Jul 25 Python
python datetime中strptime用法详解
Aug 29 Python
python实现通过队列完成进程间的多任务功能示例
Oct 28 Python
pytorch使用tensorboardX进行loss可视化实例
Feb 24 Python
Python中logger日志模块详解
Aug 04 Python
基于python图书馆管理系统设计实例详解
Aug 05 Python
Python如何获取文件路径/目录
Sep 22 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
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
Javascript注入技巧
2007/06/22 Javascript
jQuery 学习第七课 扩展jQuery的功能 插件开发
2010/05/17 Javascript
javascript ie6兼容position:fixed实现思路
2013/04/01 Javascript
jquery使用jquery.zclip插件复制对象的实例教程
2013/12/04 Javascript
JS中attr和prop属性的区别以及优先选择示例介绍
2014/06/30 Javascript
JS+CSS实现带关闭按钮DIV弹出窗口的方法
2015/02/27 Javascript
jQuery中 attr() 方法使用小结
2015/05/03 Javascript
js采用concat和sort将N个数组拼接起来的方法
2016/01/21 Javascript
终于实现了!精彩的jquery弹幕效果
2016/07/18 Javascript
Vue.js快速入门实例教程
2016/10/15 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
JavaScript+HTML5实现的日期比较功能示例
2017/07/12 Javascript
nodejs爬虫初试superagent和cheerio
2018/03/05 NodeJs
JS实现的Object数组去重功能示例【数组成员为Object对象】
2019/02/01 Javascript
vue实现固定位置显示功能
2019/05/30 Javascript
教你30秒发布一个TypeScript包到NPM的方法步骤
2019/07/22 Javascript
vue实现二级导航栏效果
2019/10/19 Javascript
JS实现百度搜索框关键字推荐
2020/02/17 Javascript
解决vue组件没显示,没起作用,没报错,但该显示的组件没显示问题
2020/09/02 Javascript
解决vue-cli输入命令vue ui没效果的问题
2020/11/17 Javascript
微信小程序实现点击导航标签滚动定位到对应位置
2020/11/19 Javascript
[01:02:34]TFT vs VGJ.T Supermajor 败者组 BO3 第二场 6.5
2018/06/06 DOTA
Django实现图片文字同时提交的方法
2015/05/26 Python
python安装教程 Pycharm安装详细教程
2017/05/02 Python
Python循环中else,break和continue的用法实例详解
2019/07/11 Python
python tkinter库实现气泡屏保和锁屏
2019/07/29 Python
Python3并发写文件与Python对比
2019/11/20 Python
Python post请求实现代码实例
2020/02/28 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
conda安装tensorflow和conda常用命令小结
2021/02/20 Python
CSS3 input框的实现代码类似Google登录的动画效果
2020/08/04 HTML / CSS
HTML5 Canvas 绘图——使用 Canvas 绘制图形图文教程 使用html5 canvas 绘制精美的图
2015/08/31 HTML / CSS
六年级学生期末评语
2014/12/26 职场文书
检讨书怎么写
2015/01/23 职场文书
事业单位聘任报告
2015/03/02 职场文书