python创意数字游戏-用Python开发一个简单的猜数字游戏

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

游戏规则

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

5242d5ed2b723c94bd16010b639366b2.jpg-wh_651x-s_306629559.jpg

准备好Python3

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

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

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

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

编码

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

fromkeras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D

# First, we needtoimport the'random'module.

# This module containsthe functionality we needtobe abletorandomlyselectthe winning number.

import random

# Now, we need toselecta random number.

# This line will setthe variable'correct'tobe equaltoa randomintegerbetween1and10.

correct = random.randint(1, 10)

# Let's get the user'sfirstguess using the'input'function.

guess = input("Enter your guess: ")

# Rightnow, theuser's inputisformattedasa string.

# We can format it asanintegerusing the'int'function.

guess = int(guess)

# Let's start a loop that will continueuntil theuserhas guessed correctly.

# We can use the '!='operatortomean'not equal'.

while guess != correct:

# Everything inthis loop will repeat until theuserhas guessed correctly.

# Let's start by giving the user feedback on their guess. We can do this using the 'if' statement.

# This statement will checkif a comparisonistrue.

# If it is, the code inside the'if'statement will run.

if guess > correct:

# This code will run if the userguessed too high.

# We can show a message totheuserusing the'print'function.

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

else:

# The 'else'statement addsontoan'if'statement.

# It will run if the condition ofthe'if'statementisfalse.

# Inthiscase, it will run if theuserguessed too low, so we can give them feedback.

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

# Now we need tolet theuserguess again.

# Notice how I am combining the two lines ofguessing codetomake 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.")

【编辑推荐】

【责任编辑:华轩 TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值