迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Python 猜谜游戏

作者:迹忆客 最近更新:2023/07/02 浏览次数:

本篇文章将介绍用 Python 开发一个简单的猜数字游戏的过程。


猜谜游戏机制

我们正在尝试开发一款游戏,从用户处获取上限和下限,生成该范围内的随机数,要求用户猜测该数字,并计算用户猜测正确的次数。 该游戏将仅基于 CLI。

Python 中随机模块的猜数游戏

我们的程序所做的第一件事是将用户的上限和下限作为输入。 这可以通过 Python 的内置 input() 方法来完成。

input() 方法从命令行读取输入并将其作为字符串返回。 这里唯一的问题是我们想要输入整数值。

我们可以将 input() 方法包装在内置 int() 方法中来解决该问题。 这会将 input() 方法返回的输入字符串转换为整数值。

以下示例显示了此步骤的有效实现。

代码:

lower_limit = int(input("Please enter the Lower Limit"))
upper_limit = int(input("Please enter the Upper Limit"))
print("Lower Limit =", lower_limit)
print("Upper Limit =", upper_limit)

输出:

Please enter the Lower Limit0
Please enter the Upper Limit99
Lower Limit = 0
Upper Limit = 99

我们可以在 input() 方法中输入数据作为输入参数,同时编写想要向用户显示的消息。 由于我们有下限和上限,因此我们可以轻松编写一些代码来生成该范围内的随机整数。

我们可以使用 Python 中内置的 random 模块来执行此任务,称为 random.randint() 方法。 它将下限和上限作为输入参数,并返回该范围内的整数。

下一个代码示例展示了如何使用 Python 的 random.randint() 方法生成指定范围内的随机整数。

代码:

import random
number = random.randint(lower_limit, upper_limit)
print("Random Number =", number)

输出:

Random Number = 47

到目前为止,我们已经从用户那里获取了限制,并在这些限制内生成了一个随机整数。 我们必须接受用户的猜测并将其与随机生成的数字进行比较。

这可以通过将 input() 方法与简单的 if/else 块相结合来实现。

代码:

guess = int(input("Guess the number"))
if guess == number:
    print("Yes, You are correct")
else:
    print("Incorrect Answer!")

输出:

Guess the number15
Incorrect Answer!

这里唯一的问题是它没有给我们猜测正确数字的线索。 它告诉我们是对还是错,这并不是一种有趣的游戏方式。

我们可以通过放置多个 if 语句并在循环内执行它们来改进这一点,直到用户猜到正确的数字。

代码:

win = False
while win != True:
    guess = int(input("Guess the number"))
    if guess == number:
        win = True
        print("Yes, You are correct")
    elif guess < number:
        print("You are a little shorter")
    else:
        print("You are a little larger")

输出:

Guess the number5
You are a little shorter
Guess the number95
You are a little larger
Guess the number47
Yes, You are correct

我们使用了 while 循环,因为我们不知道用户需要进行多少次尝试才能得到正确的答案。 我们创建了一个标志变量 win 来告诉 while 循环何时停止,并且 win 变量设置为 False 直到用户猜到正确的数字。

我们的猜数字游戏已基本完成,唯一缺少的是分数计数器,用于计算用户在得出正确答案时进行的尝试次数。 我们可以修改之前的代码并在循环中使用计数器变量。

下面的代码片段展示了我们如何为我们的猜数字游戏添加计分机制。

代码:

win = False
steps = 0
while win != True:
    guess = int(input("Guess the number"))
    steps += 1
    if guess == number:
        win = True
        print("Yes, You are correct")
        print("Number of Trails =", steps)
    elif guess < number:
        print("You are a little shorter")
    else:
        print("You are a little larger")

输出:

Guess the number22
You are a little shorter
Guess the number44
You are a little shorter
Guess the number47
Yes, You are correct
Number of Trails = 3

我们添加了一个步数计数器,用于跟踪用户完成游戏的尝试次数。

代码:

import random

lower_limit = int(input("Please enter the Lower Limit"))
upper_limit = int(input("Please enter the Upper Limit"))

number = random.randint(lower_limit, upper_limit)

win = False
steps = 0
while win != True:
    guess = int(input("Guess the number"))
    steps += 1
    if guess == number:
        win = True
        print("Yes, You are correct")
        print("Number of Trails =", steps)
    elif guess < number:
        print("You are a little shorter")
    else:
        print("You are a little larger")

输出:

Please enter the Lower Limit0
Please enter the Upper Limit10
Guess the number5
You are a little larger
Guess the number2
You are a little shorter
Guess the number3
You are a little shorter
Guess the number4
Yes, You are correct
Number of Trails = 4

输出显示游戏仅运行一次。 它不会让用户继续玩游戏直到感到无聊。

我们可以将整个程序包含在另一个循环中,该循环重复执行游戏,直到用户想要退出游戏。

完整代码:

import random

play = True
while play == True:
    lower_limit = int(input("Please enter the Lower Limit"))
    upper_limit = int(input("Please enter the Upper Limit"))

    number = random.randint(lower_limit, upper_limit)

    win = False
    steps = 0
    while win != True:
        guess = int(input("Guess the number"))
        steps += 1
        if guess == number:
            win = True
            print("Yes, You are correct")
            print("Number of Trails =", steps)
        elif guess < number:
            print("You are a little shorter")
        else:
            print("You are a little larger")

    replay = int(input("Enter -1 to replay the game."))
    if replay != -1:
        play = False

输出:

Please enter the Lower Limit1
Please enter the Upper Limit3
Guess the number2
You are a little larger
Guess the number1
Yes, You are correct
Number of Trails = 2
Enter -1 to replay the game.-1
Please enter the Lower Limit1
Please enter the Upper Limit3
Guess the number2
You are a little larger
Guess the number1
Yes, You are correct
Number of Trails = 2
Enter -1 to replay the game.0

我们创建了另一个标志变量 play,告诉我们的外部循环或主循环何时停止执行。 如果用户提供除-1之外的任何数字,外循环将停止执行,假设用户已经厌倦了重复玩这个游戏。

这是一个非常容易实现的游戏。 我们仅导入 random 模块来在代码中生成随机数。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便