如何计算数字猜测游戏的准确性?
我不知道怎么正确计算我需要多少次尝试和在这些尝试中找到这个数字的可能性之间的准确度。比如说,如果我只需要1次尝试,那准确度应该是100%。但如果需要更多的尝试,准确度就会降低。
我尝试的方法是把尝试的次数除以可能的最大选项数量,然后乘以100,这样就能得到一个1到100的准确度分数。
import random
input("You have to guess a number between two others you define! To play, press enter")
play = "yes"
lowest = 1
while play == "yes":
numIsSet = False
while not numIsSet:
highest = int(input("What should the highest number be? "))
if highest <= lowest:
print("Highest number should be greater than 1!")
else:
numIsSet = True
numSearch = random.randint(lowest, highest)
chance = lowest / highest * 100
numGuess = None
triesCount = 0
while numGuess != numSearch:
numGuess = int(input("Guess a number between " + str(lowest) + " and " + str(highest) + "! "))
triesCount += 1
if numGuess == numSearch:
accuracy = (triesCount / highest) * 100
print("Cool! You found the correct number with " + str(triesCount) + " tries!")
print("There was a chance of " + str(chance) + "% to find the correct number at the first try.")
print("Your accuracy score is:", accuracy)
if accuracy > 89:
print("★★★★★ Your score is VERY good!")
elif accuracy > 70:
print("★★★★☆ Your score is good!")
elif accuracy > 50:
print("★★★☆☆ Your score is ok!")
elif accuracy > 30:
print("★★☆☆☆ Your score isn't that good. You can do it better!")
else:
print("★☆☆☆☆ Your score is bad. You can do it a lot better!")
triesCount = 0
play = input("Do you want to play again? If yes, type 'yes', if not press enter to close the program! ")
print()
elif numGuess < lowest or numGuess > highest:
print("Please use numbers in the given range!")
elif numGuess < numSearch:
print("↑ The searched number is HIGHER than yours!")
else:
print("↓ The searched number is LOWER than yours!")
编辑
我找到了一个接近我想要的解决方案。我首先用math.log2(highest - lowest + 1) + 1
来定义平均需要的尝试次数。当数字正确时,我用accuracy = round(average / triesCount, 3)
来计算平均值和实际尝试次数之间的关系(四舍五入到小数点后3位)。1是平均分数,分数越高,说明你做得越好。
import random
import math
print("You have to guess a number between two others you define! To play, press enter")
play = "yes"
lowest = 1
while play == "yes":
numIsSet = False
while not numIsSet:
highest = int(input("What should the highest number be? "))
if highest <= lowest:
print("Highest number should be greater than 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("Guess a number between " + str(lowest) + " and " + str(highest) + "! "))
triesCount += 1
if numGuess == numSearch:
accuracy = round(average / triesCount, 3)
print()
print("Cool! You found the correct number with " + str(triesCount) + " tries!")
print("There was a chance of " + str(chance) + "% to find the correct number at the first try.")
print(f"Your accuracy score is {accuracy} (The average score is 1. The higher your score, the better it is!)")
if accuracy > 1:
print("★★★★★ Your score is VERY good!")
elif accuracy >= 0.9:
print("★★★★☆ Your score is good!")
elif accuracy >= 0.7:
print("★★★☆☆ Your score is ok!")
elif accuracy >= 0.5:
print("★★☆☆☆ Your score isn't that good. You can do it better!")
else:
print("★☆☆☆☆ Your score is bad. You can do it a lot better!")
triesCount = 0
print()
play = input("Do you want to play again? If yes, type 'yes', if not press enter to close the program! ")
print()
elif numGuess < lowest or numGuess > highest:
print("Please use numbers in the given range!")
elif numGuess < numSearch:
print("↑ The searched number is HIGHER than yours!")
lowest = numGuess
else:
print("↓ The searched number is LOWER than yours!")
highest = numGuess
1 个回答
0
我的方法是用这个公式:100 - ((100/(最高值 - 最低值) + 1) * 尝试次数)。
举个例子,如果尝试次数是3,最高值是20,最低值是1,那么计算过程是这样的:100除以20等于5,然后5乘以3等于15,最后100减去15等于85。所以准确率是85%。
这可能是实现准确率的最简单方法。