如何利用Python开发一个简单的猜数字游戏
作者:读芯术 发布时间:2022-05-21 20:38:08
前言
本文介绍如何使用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中,注释以#开头并一直持续到行结束。
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()函数中所说的内容。你的代码想怎么写都可以。
运行程序
根据你的操作系统,打开命令提示符(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.")
来源:https://juejin.im/post/5d8701d3e51d453c11684d1f


猜你喜欢
- 1 输出大写字母、小写字母、大小写字母、数字、大小写字母和数字1.1输出小写:找到小写a(97)到z(122)的的ASCII码,然后转义为字
- 数据库中对数据的操作是一大重要技能,其中,数据的恢复和还原也是常做的事。不知你是否在数据库恢复时遇到诸如“设备激活错误,请使用with mo
- 本文实例讲述了js控制多图左右滚动切换效果。分享给大家供大家参考。具体如下:这是一款纯js实现点击左右按钮图片自动左右平滑滚动,默认5个一组
- 说明1、当函数的参数太多,需要简化时,使用functools.partial可以创建一个新的函数。2、这个新的函数可以固定原始函数的部分参数
- 这篇文章主要介绍了简单了解python字符串前面加r,u的含义,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 方案有很多种,我这里简单说一下:1. into outfileSELECT * FROM mytable  
- 获取一组radio被选中项的值var item = $(’input[@name=items][@checke
- 前言python数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间。下面话不多说,来看看详细的介绍吧
- 前端开发中两个很不错的小技巧, CSS三角形与圆角背景. 的确, 它们都可以通过图片来实现, 但, 抛开用代码实现可以减小图片加载量不说,
- 1 介绍U-Net最初是用来对医学图像的语义分割,后来也有人将其应用于其他领域。但大多还是用来进行二分类,即将原始图像分成两个灰度级或者色度
- sort() 函数用于对数组单元从低到高进行排序。rsort() 函数用于对数组单元从高到低进行排序。asort() 函数用于对数组单元从低
- 首先说下Golang中的结构体,结构体是由一系列具有相同类型或不同类型的数据构成的数据集合,Golang中使用关键字struct来创建一个结
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&
- 保存为.py文件后 运行脚本在后面添加进程名称即可 比如:python proinfo.py qq 即可获取QQ的进程信息,注意不区分大小写
- 最初的声明方式在没有@property修饰的情况下,需要分别声明get、set、delete函数,然后初始化property类,将这些方法加
- 1.php安装。2.下载redis并编译(最好是在 /usr/local目录下运行该命令)# wget http://download.re
- 本文实例讲述了MySQL切分查询用法。分享给大家供大家参考,具体如下:对于大查询有时需要‘分而治之',将大查询切分为小查询: 每个查
- 如果你之前没用过进度条,八成是觉得它会增加不必要的复杂性或者很难维护,其实不然。要加一个进度条其实只需要几行代码。在这几行代码中,我们可以看
- 一直都是简单去js实现cookie的一些操作,今天把js对cookie操作系统的整理了一遍,包括:js读取cookie,js添加cookie
- Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用。当存储过程执行一次后,可以将语句缓存中,这样下次执行的