MCPcopy Create free account
hub / github.com/geekcomputers/Python / play_turn

Function play_turn

RandomNumberGame.py:48–78  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

46
47
48def 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
81def play_game() -> Tuple[List[str], List[int], List[str]]:

Callers 1

play_gameFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected