You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于尝试次数与猜中概率计算猜数字游戏的准确率?

猜数字游戏:尝试次数与准确率的计算方案

我不清楚如何正确计算猜数字游戏中,尝试次数与该次数内猜中概率之间的准确率。要求1次猜中应为100%准确率,尝试次数越多准确率越低。

最初实现方案

最初的思路是将尝试次数除以最大可选数字后乘以100,得到1-100的准确率分数,对应的Python代码如下:

import random

input("你需要猜一个你设定的两个数字之间的数!开始游戏请按回车")
play = "yes"

lowest = 1

while play == "yes":
    numIsSet = False

    while not numIsSet:
        highest = int(input("请设定最大数:"))
        if highest <= lowest:
            print("最大数必须大于1!")
        else:
            numIsSet = True
    numSearch = random.randint(lowest, highest)
    chance = lowest / highest * 100
    numGuess = None
    triesCount = 0

    while numGuess != numSearch:
        numGuess = int(input(f"请猜一个{lowest}到{highest}之间的数!"))
        triesCount += 1
        if numGuess == numSearch:
            accuracy = (triesCount / highest) * 100
            print(f"太棒了!你用了{triesCount}次就猜中了正确数字!")
            print(f"第一次就猜中的概率为{chance}%。")
            print("你的准确率分数为:", accuracy)
            if accuracy > 89:
                print("★★★★★   你的分数非常优秀!")
            elif accuracy > 70:
                print("★★★★☆   你的分数很不错!")
            elif accuracy > 50:
                print("★★★☆☆   你的分数尚可!")
            elif accuracy > 30:
                print("★★☆☆☆   你的分数不太理想,加油!")
            else:
                print("★☆☆☆☆   你的分数较差,需要多加练习!")
            triesCount = 0
            play = input("是否要继续游戏?是请输入'yes',否则按回车退出程序!")
            print()
        elif numGuess < lowest or numGuess > highest:
            print("请输入指定范围内的数字!")
        elif numGuess < numSearch:
            print("↑  目标数字比你猜的大!")
        else:
            print("↓  目标数字比你猜的小!")

优化后的方案

之后找到更接近需求的解决方案:先用math.log2(highest - lowest + 1) + 1计算平均尝试次数,猜中时用accuracy = round(average / triesCount, 3)计算准确率(保留3位小数),1为平均分,数值越高表现越好,优化后的Python代码如下:

import random
import math

print("你需要猜一个你设定的两个数字之间的数!开始游戏请按回车")
play = "yes"

lowest = 1

while play == "yes":
    numIsSet = False

    while not numIsSet:
        highest = int(input("请设定最大数:"))
        if highest <= lowest:
            print("最大数必须大于1!")
        else:
            numIsSet = True
    numSearch = random.randint(lowest, highest)
    chance = round(lowest / highest * 100, 3)
    average = math.log2(highest - lowest + 1) + 1
    print(average)
    numGuess = None
    triesCount = 0

    while numGuess != numSearch:
        numGuess = int(input(f"请猜一个{lowest}到{highest}之间的数!"))
        triesCount += 1
        if numGuess == numSearch:
            accuracy = round(average / triesCount, 3)
            print()
            print(f"太棒了!你用了{triesCount}次就猜中了正确数字!")
            print(f"第一次就猜中的概率为{chance}%。")
            print(f"你的准确率分数为{accuracy}(平均分是1,分数越高表现越好!)")
            if accuracy > 1:
                print("★★★★★   你的分数非常优秀!")
            elif accuracy >= 0.9:
                print("★★★★☆   你的分数很不错!")
            elif accuracy >= 0.7:
                print("★★★☆☆   你的分数尚可!")
            elif accuracy >= 0.5:
                print("★★☆☆☆   你的分数不太理想,加油!")
            else:
                print("★☆☆☆☆   你的分数较差,需要多加练习!")
            triesCount = 0
            print()
            play = input("是否要继续游戏?是请输入'yes',否则按回车退出程序!")
            print()
        elif numGuess < lowest or numGuess > highest:
            print("请输入指定范围内的数字!")
        elif numGuess < numSearch:
            print("↑  目标数字比你猜的大!")
            lowest = numGuess
        else:
            print("↓  目标数字比你猜的小!")
            highest = numGuess

内容的提问来源于stack exchange,提问作者craftXplorer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:05:33