如何利用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 相关文章推荐
利用QT写一个极简单的图形化Python闹钟程序
Apr 07 Python
在服务器端实现无间断部署Python应用的教程
Apr 16 Python
Tensorflow环境搭建的方法步骤
Feb 07 Python
详解python实现识别手写MNIST数字集的程序
Aug 03 Python
Python反爬虫技术之防止IP地址被封杀的讲解
Jan 09 Python
Python实现的在特定目录下导入模块功能分析
Feb 11 Python
对python 树状嵌套结构的实现思路详解
Aug 09 Python
如何基于Python批量下载音乐
Nov 11 Python
解决python-docx打包之后找不到default.docx的问题
Feb 13 Python
使用Python构造hive insert语句说明
Jun 06 Python
keras得到每层的系数方式
Jun 15 Python
python3通过subprocess模块调用脚本并和脚本交互的操作
Dec 05 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编写大型网站问题集
2007/03/06 PHP
PHP传值到不同页面的三种常见方式及php和html之间传值问题
2015/11/19 PHP
非常漂亮的JS代码经典广告
2007/10/21 Javascript
父元素与子iframe相互获取变量和元素对象的具体实现
2013/10/15 Javascript
多选列表框动态添加,移动,删除,全选等操作的简单实例
2014/01/13 Javascript
JavaScript获取元素尺寸和大小操作总结
2015/02/27 Javascript
jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表
2015/08/26 Javascript
jQuery插件开发汇总
2016/05/15 Javascript
有关suggest快速删除后仍然出现下拉列表的bug问题
2016/12/02 Javascript
详解Angular的内置过滤器和自定义过滤器【推荐】
2016/12/26 Javascript
js实现图片加载淡入淡出效果
2017/04/07 Javascript
详解Vue使用 vue-cli 搭建项目
2017/04/20 Javascript
vue基于element-ui的三级CheckBox复选框功能的实现代码
2018/10/15 Javascript
详解vue中axios的使用与封装
2019/03/20 Javascript
Vue使用.sync 实现父子组件的双向绑定数据问题
2019/04/04 Javascript
新手如何快速理解js异步编程
2019/06/24 Javascript
layer.js之回调销毁对话框的例子
2019/09/11 Javascript
使用vscode快速建立vue模板过程详解
2019/10/10 Javascript
详细分析vue响应式原理
2020/06/22 Javascript
vue 获取到数据但却渲染不到页面上的解决方法
2020/11/19 Vue.js
手把手教你如何编译打包video.js
2020/12/09 Javascript
Vue 事件的$event参数=事件的值案例
2021/01/29 Vue.js
计算Python Numpy向量之间的欧氏距离实例
2020/05/22 Python
python如何绘制疫情图
2020/09/16 Python
Django多个app urls配置代码实例
2020/11/26 Python
NFL官方在线商店:NFLShop
2020/07/29 全球购物
竞选班干部演讲稿500字
2014/08/20 职场文书
大学生党员批评与自我批评范文
2014/10/14 职场文书
党的群众路线教育实践活动党员个人整改措施
2014/10/27 职场文书
廉政承诺书范文
2015/04/28 职场文书
JavaScript实现简单图片切换
2021/04/29 Javascript
python简单验证码识别的实现过程
2021/06/20 Python
详解Oracle数据库中自带的所有表结构(sql代码)
2021/11/20 Oracle
python字符串的一些常见实用操作
2022/04/06 Python
Python通用验证码识别OCR库ddddocr的安装使用教程
2022/07/07 Python
Redis sentinel哨兵集群的实现步骤
2022/07/15 Redis