-
Notifications
You must be signed in to change notification settings - Fork 9
/
number_guess.py
111 lines (80 loc) · 2.8 KB
/
number_guess.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Number Guessing Game
import sys
import random
sys.path.append('src/day_9')
from logo import LOGO
from auction_program import clear_console
from colorama import init, Fore
def handle_exception(func, error_message):
try:
return func()
except (AssertionError, ValueError):
clear_console()
print(Fore.RED + error_message)
return handle_exception(func, error_message)
def get_player_guess():
player_guess = int(input("Make a guess: ").strip())
assert 1 <= player_guess <= 100
return player_guess
def choose_difficulty():
print(
f"Choose a mode: {Fore.BLUE} (1) Easy - {Fore.LIGHTGREEN_EX} (2) Hard - {Fore.RED} (3) Expert: "
)
choice = int(input().strip())
mode = "Easy" if choice == 1 else "Hard" if choice == 2 else "Expert"
print(f"{mode} mode activated! Good luck!\n")
assert choice == 1 or choice == 2 or choice == 3
return choice
def computer_picks_random_number(start, end):
return random.randint(start, end)
def welcome():
print("Welcome to the Number Guessing Game\n")
def get_player_attempts_by_difficulty(mode):
# key: user difficulty choice choice, value: total attempts
modes = {1: 10, 2: 5, 3: 3}
return modes[mode]
def print_user_attempts(player_attempts):
color = Fore.RED if player_attempts <= 3 else Fore.YELLOW
word = 'attempts' if player_attempts > 1 else 'attempt'
print(
color +
f"You have {str(player_attempts)} {word} remaining to guess the number\n"
)
def play_again():
choice = input("Press enter to play again!").strip()
play_number_guessing_game() if not choice else sys.exit()
def play_number_guessing_game():
start, end, count = 1, 100, 0
welcome()
target = computer_picks_random_number(start, end)
mode = handle_exception(choose_difficulty, "Please enter a valid mode")
player_attempts = get_player_attempts_by_difficulty(mode)
print(f"I'm thinking of a number from {start} to {end}\n")
while True:
if player_attempts == 0:
print(
f"No more attempts left, the number was {Fore.BLUE + str(target)}"
)
break
player_guess = handle_exception(
get_player_guess, f"Please enter a valid number ({start} - {end})")
if target > player_guess:
print(Fore.GREEN + "Too Low")
elif target < player_guess:
print(Fore.YELLOW + "Too High")
else:
print(f"Correct! The number was {target}")
break
count += 1
player_attempts -= 1
print_user_attempts(player_attempts)
play_again()
def main():
init(autoreset=True)
print(Fore.RED + LOGO)
try:
play_number_guessing_game()
except KeyboardInterrupt:
sys.exit()
if __name__ == "__main__":
main()