File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ import random
2
+
3
+ def guessing_game ():
4
+ print ("Welcome to the Number Guessing Game!" )
5
+ lower_bound = 1
6
+ upper_bound = 100
7
+ secret_number = random .randint (lower_bound , upper_bound )
8
+ attempts = 0
9
+ max_attempts = 10
10
+
11
+ print (f"I've selected a number between { lower_bound } and { upper_bound } . You have { max_attempts } attempts to guess it." )
12
+
13
+ while attempts < max_attempts :
14
+ try :
15
+ guess = int (input ("Enter your guess: " ))
16
+ attempts += 1
17
+
18
+ if guess < lower_bound or guess > upper_bound :
19
+ print (f"Please guess a number between { lower_bound } and { upper_bound } ." )
20
+ continue
21
+
22
+ if guess < secret_number :
23
+ print ("Too low!" )
24
+ elif guess > secret_number :
25
+ print ("Too high!" )
26
+ else :
27
+ print (f"Congratulations! You've guessed the number { secret_number } in { attempts } attempts." )
28
+ break
29
+ except ValueError :
30
+ print ("Invalid input. Please enter an integer." )
31
+
32
+ if attempts == max_attempts :
33
+ print (f"Sorry, you've used all your attempts. The secret number was { secret_number } ." )
34
+
35
+ if __name__ == "__main__" :
36
+ guessing_game ()
File renamed without changes.
You can’t perform that action at this time.
0 commit comments