# Ask the user to guess a number between 1 and 10 from random import randint, seed def play(guess: int, hidden: int) -> str: if guess == hidden: return "ok" elif guess > hidden: return "smaller" else: return "bigger" def guess_hidden(min_g: int, max_g: int, hidden: int) -> int: """Return the number of tries to guess. """ tries = 0 result = "" while result != "ok": tries += 1 mid = (max_g + min_g) // 2 result = play(mid, hidden) if result == "ok": return tries if result == "smaller": max_g = mid else: min_g = mid seed(7789798) h = randint(1, 10**9) t = guess_hidden(1, 10**9, h) print("The hidden number was guessed in " + str(t) + " tries.")
Last modified
2 years ago
Last modified on Nov 3, 2022, 10:45:57 AM
Note:
See TracWiki
for help on using the wiki.