Skip to content

Commit 28791ec

Browse files
authored
Merge pull request #243 from ZerubbabelT/master
Improved Number Guessing Game: Added Difficulty Levels, Input Validation, and User-Friendly Messages
2 parents f2f740a + 632f8fa commit 28791ec

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

Number Guessing Game/main.py

+64-31
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,76 @@
11
import random
22

3-
"""
4-
Funtion start() create the magic number, number of attemps, and ask user for input
5-
"""
63
def start():
7-
global magic_number
8-
magic_number = random.randint(1, 100)
9-
10-
global attempts
4+
"""
5+
Creates the magic number, number of attempts, and asks the user for input.
6+
"""
7+
global magic_number, attempts
8+
magic_number = choose_difficulty()
119
attempts = 0
12-
1310
print('---------------------------')
14-
print('Guess a number between 1 and 100')
11+
print('Guess the magic number!')
12+
print('Try to guess the number in as few attempts as possible.')
13+
14+
def choose_difficulty():
15+
"""
16+
Allows the user to select the difficulty level and returns the corresponding range for the magic number.
17+
"""
18+
print("Choose difficulty level:")
19+
print("1. Easy (1-50)")
20+
print("2. Medium (1-100)")
21+
print("3. Hard (1-200)")
22+
23+
while True:
24+
level = input("Enter 1, 2, or 3: ")
25+
if level == '1':
26+
return random.randint(1, 50)
27+
elif level == '2':
28+
return random.randint(1, 100)
29+
elif level == '3':
30+
return random.randint(1, 200)
31+
else:
32+
print("Invalid choice. Please enter 1, 2, or 3.")
1533

16-
# Function that checks if player won, if it won, returns True
1734
def check_win(player_guess):
35+
"""
36+
Checks if the player’s guess is correct, too high, or too low.
37+
"""
1838
if player_guess > magic_number:
19-
print('Too big...')
39+
print('Too high! Try a smaller number.')
2040
elif player_guess < magic_number:
21-
print('Too small')
41+
print('Too low! Try a larger number.')
2242
elif player_guess == magic_number:
2343
return True
2444

25-
start()
26-
27-
# Game loop
28-
while True:
29-
# Take the player input
30-
guess = int(input())
31-
attempts += 1
32-
33-
if check_win(guess):
34-
print('You won! - Number of attempts: ' + str(attempts))
35-
36-
keep_playing = input('Keep playing?(y\\n)')
37-
# If player want to keep the game, reset the number of attempts
38-
if keep_playing == 'y':
39-
attempts = 0
40-
start()
41-
# If player don't want to keep playing, quit the game
42-
elif keep_playing == 'n':
43-
quit()
45+
def play_game():
46+
"""
47+
Main game loop that starts the game, processes user input, and checks for win conditions.
48+
"""
49+
global attempts
50+
start()
51+
52+
while True:
53+
try:
54+
guess = int(input('Enter your guess: '))
55+
if guess < 1 or guess > 200:
56+
print("Please enter a number between 1 and 200.")
57+
continue
58+
except ValueError:
59+
print("Invalid input. Please enter a valid number.")
60+
continue
61+
62+
attempts += 1
63+
64+
if check_win(guess):
65+
print(f'Congratulations! You guessed the number in {attempts} attempts.')
66+
keep_playing = input('Would you like to play again? (y/n): ')
67+
if keep_playing.lower() == 'y':
68+
play_game()
69+
elif keep_playing.lower() == 'n':
70+
print("Thanks for playing! Have a great day!")
71+
break
72+
else:
73+
print("Invalid input. Exiting the game.")
74+
break
75+
76+
play_game()

0 commit comments

Comments
 (0)