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

 更新时间:2019年09月22日 15:05:26   作者:读芯术  
这篇文章主要给大家介绍了关于如何利用Python开发一个简单的猜数字游戏的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用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:

# 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.")

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

相关文章

  • python数据持久存储 pickle模块的基本使用方法解析

    python数据持久存储 pickle模块的基本使用方法解析

    这篇文章主要介绍了python数据持久存储 pickle模块的基本使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • pycharm出现No pyvenv.cfg file错误的问题解决

    pycharm出现No pyvenv.cfg file错误的问题解决

    本文主要介绍了pycharm出现No pyvenv.cfg file错误的问题解决,主要是通过恢复历史记录中的未删除状态来解决,下面就来详细的介绍一下,感兴趣的可以了解一下
    2025-05-05
  • 基于Python实现在控制台查看excel的内容

    基于Python实现在控制台查看excel的内容

    这篇文章主要为大家详细介绍了如何基于Python实现在控制台查看excel的内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12
  • Python3使用requests包抓取并保存网页源码的方法

    Python3使用requests包抓取并保存网页源码的方法

    这篇文章主要介绍了Python3使用requests包抓取并保存网页源码的方法,实例分析了Python3环境下requests模块的相关使用技巧,需要的朋友可以参考下
    2016-03-03
  • Python Django切换MySQL数据库实例详解

    Python Django切换MySQL数据库实例详解

    这篇文章主要介绍了Python Django切换MySQL数据库实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python 实现多维数组(array)排序

    python 实现多维数组(array)排序

    今天小编就为大家分享一篇python 实现多维数组(array)排序,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python 矩阵增加一行或一列的实例

    python 矩阵增加一行或一列的实例

    下面小编就为大家分享一篇python 矩阵增加一行或一列的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • keras 使用Lambda 快速新建层 添加多个参数操作

    keras 使用Lambda 快速新建层 添加多个参数操作

    这篇文章主要介绍了keras 使用Lambda 快速新建层 添加多个参数操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • pytorch版本PSEnet训练并部署方式

    pytorch版本PSEnet训练并部署方式

    这篇文章主要介绍了pytorch版本PSEnet训练并部署方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Python 如何引用不确定的函数

    Python 如何引用不确定的函数

    在Python中,引用不确定的函数通常意味着我们可能在运行时才知道要调用哪个函数,或者我们可能想根据某些条件动态地选择不同的函数来执行,下面给大家分享Python 如何引用不确定的函数,感兴趣的朋友跟随小编一起看看吧
    2024-07-07

最新评论