Day 14 – Higher Lower 游戏

Day 14 – Higher Lower 游戏

温馨提示:本文最后更新于2024-12-18 18:16:11,某些文章具有时效性,若有错误或已失效,请在下方留言

问题解决流程

问题解决流程

游戏实现

游戏的视线效果,如下所示

分解任务

任务分解完成后,形成 Todo 列表,如下图所示

Todo 列表

任务实现

显示 ASCII 艺术字符

# 显示 ASCII艺术字符
from art import logo
print(logo)

运行代码,在输出控制台显示 ASCII 艺术字符。

 ASCII 艺术字符

Todo 列表中,标记次任务为完成项,如下所示

标记完成

从 game data 中随机生成一个账户

# 从 game data 中随机生成一个账户
import random
from game_data import data
account_a = random.choice(data)
account_b = random.choice(data)
if account_a == account_b:
    account_b = random.choice(data)

Todo 列表中,标记次任务为完成项,如下所示

标记完成

格式化账户信息为可打印样式

# 格式化账户信息为可打印样式
from art import vs
def format_data(account):
    account_name = account['name']
    account_description = account['description']
    account_country = account['country']
    return f"{account_name}, a {account_description}, from {account_country}"

print(f"Compare A: {format_data(account_a)}.")
print(vs)
print(f"Against B: {format_data(account_b)}.")

输出结果,如下所示

输出结果

Todo 列表中,标记次任务为完成项,如下所示

标记完成

邀请用户猜测

# 邀请用户猜测
guess = input("Who has more followers? Type 'A' or 'B': ").lower()

输出内容,如下所示

输出内容

Todo 列表中,标记次任务为完成项,如下所示

标记完成

检测用户是否正确

# 检测用户是否正确
def check_answer(user_guess, a_followers, b_followers):
    if a_followers > b_followers:
        return user_guess == 'a'
    else:
        return user_guess == 'b'

## 获取每个账户的关注数
a_followers_count = account_a['followers_count']
b_followers_count = account_b['followers_count']
## 使用 if 语句检测用户是否正确
is_correct = check_answer(guess, a_followers_count, b_followers_count)

Todo 列表中,标记次任务为完成项,如下所示

标记完成

基于用户猜的进行反馈并记录分值

# 基于用户猜的进行反馈
# 记录分值
score = 0
if is_correct:
    score += 1
    print("You are right!")
else:
    print("Sorry, that's wrong!")

Todo 列表中,标记次任务为完成项,如下所示

标记完成

使游戏可重复

# 使游戏重复
while game_should_continue:
    account_a = random.choice(data)
    account_b = random.choice(data)
    if account_a == account_b:
        account_b = random.choice(data)
    print(f"Compare A: {format_data(account_a)}.")
    print(vs)
    print(f"Against B: {format_data(account_b)}.")

    # 邀请用户猜测
    guess = input("Who has more followers? Type 'A' or 'B': ").lower()

    # 检测用户是否正确
    def check_answer(user_guess, a_followers, b_followers):
        if a_followers > b_followers:
            return user_guess == 'a'
        else:
            return user_guess == 'b'

    ## 获取每个账户的关注数
    a_followers_count = account_a['followers_count']
    b_followers_count = account_b['followers_count']
    ## 使用 if 语句检测用户是否正确
    is_correct = check_answer(guess, a_followers_count, b_followers_count)

    # 基于用户猜的进行反馈
    # 记录分值

    if is_correct:
        score += 1
        print("You are right!")
    else:
        print("Sorry, that's wrong!")
        game_should_continue = False

Todo 列表中,标记次任务为完成项,如下所示

标记完成

使 B 位置的账户成为下轮 A 位置的账户

account_b 移出循环,循环内部使 account_a = account_b

account_b = random.choice(data)

while game_should_continue:
    # 使 B 位置的账户成为下轮 A 位置的账户
    account_a = account_b
    account_b = random.choice(data)

    if account_a == account_b:
        account_b = random.choice(data)
    print(f"Compare A: {format_data(account_a)}.")
    print(vs)
    print(f"Against B: {format_data(account_b)}.")
    ...

Todo 列表中,标记次任务为完成项,如下所示

标记完成
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容