Guess the number/With feedback
{draft task}}
The task is to write a game that follows the following rules:
- The computer will choose a number between given set limits and asks the player for repeated guesses until the player guesses the target number correctly. At each guess, the computer responds with whether the guess was higher than, equal to, or less than the target - or signals that the input was inappropriate.
C.f: Guess the number/With Feedback (Player)
Python
<lang python>import random
inclusive_range = (1, 100)
print("Guess my target number that is between %i and %i (inclusive).\n"
% inclusive_range)
target = random.randint(*inclusive_range) answer, i = None, 0 while answer != target:
i += 1 txt = input("Your guess(%i): " % i) try: answer = int(txt) except ValueError: print(" I don't understand your input of '%s' ?" % txt) continue if answer < inclusive_range[0] or answer > inclusive_range[1]: print(" Out of range!") continue if answer == target: print(" Ye-Haw!!") break if answer < target: print(" Too low.") if answer > target: print(" Too high.")
print("\nThanks for playing.")</lang>
Sample Game'
Guess my target number that is between 1 and 100 (inclusive). Your guess(1): 50 Too high. Your guess(2): 25 Too low. Your guess(3): 40 Too high. Your guess(4): 30 Too low. Your guess(5): 35 Too high. Your guess(6): 33 Too high. Your guess(7): 32 Too high. Your guess(8): 31 Ye-Haw!! Thanks for playing.
Sample trapped Errors
Guess my target number that is between 1 and 100 (inclusive). Your guess(1): 0 Out of range! Your guess(2): 101 Out of range! Your guess(3): Howdy I don't understand your input of 'Howdy' ? Your guess(4):