-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguessingnumber
More file actions
34 lines (28 loc) · 1.02 KB
/
guessingnumber
File metadata and controls
34 lines (28 loc) · 1.02 KB
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
import random
def guess_the_number_game():
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
print("Welcome to the Guess the Number Game!")
print("I have selected a number between 1 and 100.")
print("Try to guess the number! Type 'exit' to quit the game.")
while True:
guess = input("Enter your guess: ")
# Allow the player to exit the game
if guess.lower() == 'exit':
print("Thank you for playing! Goodbye.")
break
try:
guess = int(guess)
except ValueError:
print("Please enter a valid number or type 'exit' to quit.")
continue
# Check the guess
if guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You've guessed the number {secret_number}!")
break
if __name__ == "__main__":
guess_the_number_game()