Let a player try to guess a random number. Args: player (str): player name Returns: int: number of attempts taken Example: >>> import builtins, random >>> random.seed(1) >>> inputs = iter(["30", "15", "9"]) >>> builtins.input = lamb
(player: str)
| 46 | |
| 47 | |
| 48 | def play_turn(player: str) -> int: |
| 49 | """ |
| 50 | Let a player try to guess a random number. |
| 51 | |
| 52 | Args: |
| 53 | player (str): player name |
| 54 | |
| 55 | Returns: |
| 56 | int: number of attempts taken |
| 57 | |
| 58 | Example: |
| 59 | >>> import builtins, random |
| 60 | >>> random.seed(1) |
| 61 | >>> inputs = iter(["30", "15", "9"]) |
| 62 | >>> builtins.input = lambda prompt="": next(inputs) |
| 63 | >>> play_turn("Alice") # doctest: +ELLIPSIS |
| 64 | 3 |
| 65 | """ |
| 66 | target = random.randint(1, 100) |
| 67 | print(f"\n{player}, it's your turn!") |
| 68 | attempts = 0 |
| 69 | while True: |
| 70 | guess = int(input("Please enter your guess: ")) |
| 71 | attempts += 1 |
| 72 | if guess > target: |
| 73 | print("Too high, try smaller...") |
| 74 | elif guess < target: |
| 75 | print("Too low, try bigger...") |
| 76 | else: |
| 77 | print("Congratulations! You guessed it!") |
| 78 | return attempts |
| 79 | |
| 80 | |
| 81 | def play_game() -> Tuple[List[str], List[int], List[str]]: |