import random


def read_guess(low, high):
    """Ask until the player types a whole number within range."""
    while True:
        raw = input(f"Guess ({low}-{high}): ")
        try:
            guess = int(raw)
        except ValueError:
            print("Please type a whole number.")
            continue
        if guess < low or guess > high:
            print(f"Stay between {low} and {high}.")
            continue
        return guess


def play_round(low=1, high=100):
    secret = random.randint(low, high)
    attempts = 0
    while True:
        guess = read_guess(low, high)
        attempts += 1
        if guess < secret:
            print("Higher!")
        elif guess > secret:
            print("Lower!")
        else:
            print(f"You got it in {attempts} tries!")
            return attempts


def main():
    print("I'm thinking of a number between 1 and 100.")
    best = None
    while True:
        attempts = play_round()
        if best is None or attempts < best:
            best = attempts
        print(f"Best so far: {best} tries.")
        again = input("Play again? (y/n): ").strip().lower()
        if again != "y":
            print("Thanks for playing!")
            break


if __name__ == "__main__":
    main()
