1
1
import random
2
2
3
3
def guessing_game ():
4
- print ("Welcome to the Number Guessing Game!" )
5
- lower_bound = 1
6
- upper_bound = 100
4
+ # Welcome and difficulty level introduction
5
+ print ("Welcome to the Enhanced Number Guessing Game!" )
6
+
7
+ # Adding difficulty levels (Easy, Medium, Hard)
8
+ print ("Choose a difficulty level: " )
9
+ print ("1. Easy (1-50, 15 attempts)" )
10
+ print ("2. Medium (1-100, 10 attempts)" )
11
+ print ("3. Hard (1-200, 7 attempts)" )
12
+
13
+ # Input to select difficulty level
14
+ while True :
15
+ try :
16
+ difficulty = int (input ("Enter 1, 2, or 3 for difficulty: " ))
17
+ if difficulty == 1 :
18
+ lower_bound = 1
19
+ upper_bound = 50
20
+ max_attempts = 15
21
+ break
22
+ elif difficulty == 2 :
23
+ lower_bound = 1
24
+ upper_bound = 100
25
+ max_attempts = 10
26
+ break
27
+ elif difficulty == 3 :
28
+ lower_bound = 1
29
+ upper_bound = 200
30
+ max_attempts = 7
31
+ break
32
+ else :
33
+ print ("Invalid choice. Please select a valid difficulty (1, 2, or 3)." )
34
+ except ValueError :
35
+ print ("Invalid input. Please enter a number (1, 2, or 3)." )
36
+
7
37
secret_number = random .randint (lower_bound , upper_bound )
8
38
attempts = 0
9
- max_attempts = 10
39
+ score = 100 # Initial score for tracking how well the user performs
10
40
11
- print (f"I 've selected a number between { lower_bound } and { upper_bound } . You have { max_attempts } attempts to guess it." )
41
+ print (f"\n I 've selected a number between { lower_bound } and { upper_bound } . You have { max_attempts } attempts to guess it." )
12
42
13
43
while attempts < max_attempts :
14
44
try :
@@ -24,13 +54,42 @@ def guessing_game():
24
54
elif guess > secret_number :
25
55
print ("Too high!" )
26
56
else :
27
- print (f"Congratulations! You've guessed the number { secret_number } in { attempts } attempts." )
57
+ print (f"🎉 Congratulations! You've guessed the number { secret_number } in { attempts } attempts!" )
58
+ print (f"Your final score is { score - (attempts * 5 )} points!" ) # Subtract points based on attempts
28
59
break
60
+
61
+ # Adding hints after incorrect guesses
62
+ if guess != secret_number :
63
+ if secret_number % 2 == 0 :
64
+ print ("Hint: The secret number is even." )
65
+ else :
66
+ print ("Hint: The secret number is odd." )
67
+
68
+ difference = abs (secret_number - guess )
69
+ if difference > 50 :
70
+ print ("Hint: You're way off! More than 50 away!" )
71
+ elif difference > 20 :
72
+ print ("Hint: Getting warmer! You're 20-50 away." )
73
+ elif difference > 10 :
74
+ print ("Hint: You're within 10-20 of the number!" )
75
+ else :
76
+ print ("Hint: Very close! You're within 10 of the number!" )
77
+
29
78
except ValueError :
30
79
print ("Invalid input. Please enter an integer." )
31
80
81
+ # If user has used all attempts
32
82
if attempts == max_attempts :
33
83
print (f"Sorry, you've used all your attempts. The secret number was { secret_number } ." )
84
+ print (f"Your final score is { score - (attempts * 5 )} points." )
85
+
86
+ # Adding replay option to make the game more dynamic
87
+ replay = input ("Would you like to play again? (yes/no): " ).strip ().lower ()
88
+ if replay == 'yes' :
89
+ guessing_game () # Recursively call the game function for a new round
90
+ else :
91
+ print ("Thanks for playing! Goodbye!" )
92
+
34
93
35
94
if __name__ == "__main__" :
36
95
guessing_game ()
0 commit comments