使用 Python 的 Higher-Lower 游戏

pythonserver side programmingprogramming

Higher-Lower 游戏是一款经典且有趣的猜谜游戏,要求玩家猜测指定范围内随机生成的数字。通过使用 Python 编程语言实现此游戏,初学者可以获得宝贵的编码经验,同时创造互动且愉快的游戏体验。

在本文中,我们将探索使用 Python 创建 Higher-Lower 游戏的分步过程,并在此过程中提供详细的解释和代码示例。无论您是希望提高技能的新手程序员,还是只是在寻找有趣的编码项目,本文都将指导您开发功能齐全的 Higher-Lower 游戏,该游戏将吸引和娱乐所有年龄段的玩家。

了解规则

在深入实现之前,让我们更深入地了解 Higher-Lower 游戏的规则。游戏开始时会生成一个预定义范围内的随机数,例如 1 到 100 之间。玩家的目标是猜出这个随机生成的数字。每次猜测后,游戏都会提供反馈,说明猜测的数字是高于还是低于实际数字。根据此反馈,玩家会调整后续猜测。玩家会继续猜测,直到找到正确的数字。

设置游戏

为了构建 Higher−Lower 游戏,我们必须首先建立一些准备元素。我们必须先导入随机模块,然后才能生成随机数。我们还定义了将用于生成随机数的参数。我们将下限显示为 1,上限显示为 100。我们还初始化 number_of_guesses 变量,该变量将用于跟踪玩家猜测的总次数。

import random

lower_bound = 1
upper_bound = 100
number_of_guesses = 0

生成随机数

要创建给定范围内的随机数,我们可以使用 random 模块中的 random.randint() 函数。此函数将下限和上限作为参数,并返回它们之间的随机整数。

random_number = random.randint(lower_bound, upper_bound)

获取用户输入

为了让玩家进行猜测,我们需要捕获他们的输入。我们通过使用 input() 函数来实现这一点,该函数提示玩家输入一个数字。由于 input() 函数返回一个字符串,我们使用 int() 函数将输入转换为整数。

guess = int(input("猜数字: "))

比较猜测

收到玩家的猜测后,我们需要将其与随机生成的数字进行比较。我们可以使用 if-else 语句根据比较结果提供反馈。如果猜测结果等于随机数,则玩家获胜。否则,我们会告知玩家猜测结果是否高于或低于实际数字,并提供另一次猜测机会。

if guess == random_number:
    print("Congratulations! You guessed the number correctly.")
else:
    if guess < random_number:
        print("The number is higher!")
    else:
        print("The number is lower!")
    number_of_guesses += 1

循环游戏

我们将输入和比较步骤放在一个 while 循环中,以便玩家可以多次猜测,直到找到正确的数字。我们设置了一个条件,让循环继续,直到玩家猜出正确的数字。

while guess != random_number:
    guess = int(input("Guess the number: "))
    
    if guess < random_number:
        print("The number is higher!")
    else:
        print("The number is lower!")
        
    number_of_guesses += 1

显示结果

最后,在玩家成功猜出数字后,我们会显示他们赢得游戏的尝试次数。

print("恭喜!您猜对了数字", number_of_guesses, "attempts.")

将它们放在一起

以下是 Higher−Lower 游戏的完整代码:

import random

lower_bound = 1
upper_bound = 100
number_of_guesses = 0

random_number = random.randint(lower_bound, upper_bound)

guess = int(input("Guess the number: "))

while guess != random_number:
    if guess < random_number:
        print("The number is higher!")
    else:
        print("The number is lower!")
        
    number_of_guesses += 1
    guess = int(input("Guess the number: "))

print("Congratulations! You guessed the number correctly in", number_of_guesses, "attempts.")

输出

Guess the number: 50
The number is higher!
Guess the number: 75
The number is lower!
Guess the number: 60
The number is higher!
Guess the number: 65
The number is lower!
Guess the number: 63
Congratulations! You guessed the number correctly in 4 attempts.

错误处理

我们希望确保玩家在 Higher−Lower 游戏中在设定的范围内提供正确的猜测。我们可以使用 try−except 块创建错误管理,以处理玩家输入错误值时可能出现的错误。

try:
    guess = int(input("Guess the number: "))

    if guess < lower_bound or guess > upper_bound:
        raise ValueError("Invalid guess. Please enter a number within the range.")

except ValueError as e:
    print(str(e))

在上面的代码片段中,我们使用 try 块尝试将用户的输入转换为整数。如果转换成功,我们将像以前一样继续进行比较。但是,如果在转换过程中发生错误,则会引发 ValueError。

我们使用 except 块来捕获 ValueError 并妥善处理它。在这种情况下,我们打印一条错误消息,通知玩家他们的猜测无效,并要求他们输入指定范围内的数字。

通过实施错误处理,我们可以防止由无效输入引起的崩溃或意外行为,从而改善用户体验。它还允许我们指导玩家在游戏中做出正确且有意义的猜测。

以下是更新后的带有错误处理的代码片段:

try:
    guess = int(input("Guess the number: "))

    if guess < lower_bound or guess > upper_bound:
        raise ValueError("Invalid guess. Please enter a number within the range.")

    if guess == random_number:
        print("Congratulations! You guessed the number correctly.")
    else:
        if guess < random_number:
            print("The number is higher!")
        else:
            print("The number is lower!")
        number_of_guesses += 1

except ValueError as e:
    print(str(e))

结论

总之,使用 Python 创建 Higher-Lower 游戏是一种增强编码技能的绝佳方式,同时还能提供引人入胜的互动体验。按照本文概述的步骤,您可以开发一款功能齐全的游戏,挑战玩家猜测给定范围内随机生成的数字。通过使用循环、条件语句和输入/输出函数,您已经学会了如何实现游戏的核心机制。Higher-Lower 游戏不仅提供愉快的游戏体验,而且还是编程概念的绝佳入门。因此,准备好踏上创建自己的 Higher-Lower 游戏的激动人心的旅程,并享受探索 Python 编程可能性的乐趣吧。


相关文章