-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
40 lines (33 loc) · 1.44 KB
/
game.py
File metadata and controls
40 lines (33 loc) · 1.44 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
35
36
37
38
39
40
import random
def guess_the_number():
# Computer picks a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
guessed_correctly = False
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
print("Can you guess what it is?\n")
while not guessed_correctly:
try:
# Get player's guess
guess = int(input("Enter your guess: "))
attempts += 1
if guess < secret_number:
print("Too low! Try a higher number.\n")
elif guess > secret_number:
print("Too high! Try a lower number.\n")
else:
guessed_correctly = True
print(f"Congratulations! 🎉 You guessed it!")
print(f"The number was {secret_number}.")
print(f"You took {attempts} attempts.")
# Ask if they want to play again
play_again = input("\nDo you want to play again? (yes/no): ").strip().lower()
if play_again == "yes" or play_again == "y":
guess_the_number() # Restart the game
else:
print("Thanks for playing! Goodbye! 👋")
except ValueError:
print("Please enter a valid whole number.\n")
# Start the game
guess_the_number()