python1~100猜数字游戏规则_⽤Python开发⼀个简单的猜数
字游戏
本⽂介绍如何使⽤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:
vim编辑器下载与安装
# 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.")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。