forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_guess.py
More file actions
40 lines (29 loc) · 812 Bytes
/
number_guess.py
File metadata and controls
40 lines (29 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random
def generate_puzzle(low=1, high=100):
print(f"I'm thinking of a number between {low} and {high}...")
return random.randint(low, high)
def make_guess(target):
guess = None
while guess is None:
try:
guess = int(input("Guess: "))
except ValueError:
print("Enter an integer.")
if guess == target:
return True
if guess < target:
print("Too low.")
elif guess > target:
print("Too high.")
return False
def play(tries=8):
target = generate_puzzle()
while tries > 0:
if make_guess(target):
print("You win!")
return
tries -= 1
print(f"{tries} tries left.")
print(f"Game over! The answer was {target}.")
if __name__ == '__main__':
play()