千锋教育-做有情怀、有良心、有品质的职业教育机构

400-811-9990
手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

上海
  • 北京
  • 郑州
  • 武汉
  • 成都
  • 西安
  • 沈阳
  • 广州
  • 南京
  • 深圳
  • 大连
  • 青岛
  • 杭州
  • 重庆
当前位置:广州千锋IT培训  >  技术干货  >  用Python开发一个简单的猜数字游戏

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

来源:千锋教育
发布人:xqq
时间: 2023-11-07 20:16:51

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

游戏规则

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

准备好Python3

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

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

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

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

编码

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

fromkeras.layersimportConv2D,MaxPooling2D,GlobalAveragePooling2D

#First,weneedtoimportthe'random'module.

#Thismodulecontainsthefunctionalityweneedtobeabletorandomlyselectthewinningnumber.

importrandom

#Now,weneedtoselectarandomnumber.

#Thislinewillsetthevariable'correct'tobeequaltoarandomintegerbetween1and10.

correct=random.randint(1,10)

#Let'sgettheuser'sfirstguessusingthe'input'function.

guess=input("Enteryourguess:")

#Rightnow,theuser'sinputisformattedasastring.

#Wecanformatitasanintegerusingthe'int'function.

guess=int(guess)

#Let'sstartaloopthatwillcontinueuntiltheuserhasguessedcorrectly.

#Wecanusethe'!='operatortomean'notequal'.

whileguess!=correct:

#Everythinginthisloopwillrepeatuntiltheuserhasguessedcorrectly.

#Let'sstartbygivingtheuserfeedbackontheirguess.Wecandothisusingthe'if'statement.

#Thisstatementwillcheckifacomparisonistrue.

#Ifitis,thecodeinsidethe'if'statementwillrun.

ifguess>correct:

#Thiscodewillruniftheuserguessedtoohigh.

#Wecanshowamessagetotheuserusingthe'print'function.

print("You'veguessedtoohigh.Tryguessinglower.")

else:

#The'else'statementaddsontoan'if'statement.

#Itwillruniftheconditionofthe'if'statementisfalse.

#Inthiscase,itwillruniftheuserguessedtoolow,sowecangivethemfeedback.

print("You'veguessedtoolow.Tryguessinghigher.")

#Nowweneedtolettheuserguessagain.

#NoticehowIamcombiningthetwolinesofguessingcodetomakejustoneline.

guess=int(input("Enteryourguess:"))

#Ifauser'sguessisstillincorrect,thecodeinthe'while'loopwillberepeated.

#Ifthey'vereachedthispointinthecode,itmeanstheyguessedcorrectly,solet'ssaythat.

print("Congratulations!You'veguessedcorrectly.")

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

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

运行程序

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

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

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

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

确保将C:/Users/username/Desktop/main.py替换为Python文件的完整路径。

程序运行后,可测试一下,玩几次!完成操作后,按向上箭头键复制最后一个命令,然后按Enter即可再次运行。

以下是没有任何注释的代码版本:

importrandom

correct=random.randint(1,10)

guess=input("Enteryourguess:")

guess=int(guess)

whileguess!=correct:

ifguess>correct:

print("You'veguessedtoohigh.Tryguessinglower.")

else:

print("You'veguessedtoolow.Tryguessinghigher.")

guess=int(input("Enteryourguess:"))

print("Congratulations!You'veguessedcorrectly.")

以上内容为大家介绍了用Python开发一个简单的猜数字游戏,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。http://www.mobiletrain.org/

声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。

猜你喜欢LIKE

python 列表元素的增加

2023-11-07

Python 决策树算法思想

2023-11-07

Python 枚举

2023-11-07

最新文章NEW

python 抽象类和抽象接口

2023-11-07

Python 分类技术

2023-11-07

Python C代码统计工具代码的效果验证

2023-11-07

相关推荐HOT

更多>>

快速通道 更多>>

最新开班信息 更多>>

网友热搜 更多>>