How to calculate the accuracy in number guessing game?

58 Views Asked by At

I don't know how to correctly calculate the accuracy of the relationship between how many tries I needed and how probable it was to find the number within this amount of tries. E.g. if I needed 1 try it should be 100% accuracy-score but if it took more tries, the accuracy-score gets decreased.

My attempt was to divide the amount of tries by the highest possible option and multiply this by 100 to get an accuracy-score of 1 to 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!")

Edit

I got a solution close to what I wanted. I first define the average amount of tries with math.log2(highest - lowest + 1) + 1. When the number is right, I calculate the relationship between the average and the actual amount of tries with accuracy = round(average / triesCount, 3) (rounded to 3 decimals). 1 is the average score and the higher it is, the better you are.

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

There are 1 best solutions below

1
dontbrushyourteeth On

My approach would be 100 - ((100/(highest - lowest)+1))(triescount)) for example triescount = 3, highest = 20, lowest = 1, 100/20 = 5, 53 = 15, 100 - 15 =85 so 85% accuracy

This is probably the easiest way of implementing accuracy