forked from Open-Source-you/Hackotberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess the number.py
34 lines (28 loc) · 1014 Bytes
/
Guess the number.py
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 number_guessing_game():
# Generate a random number between 1 and 100
number_to_guess = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I have picked a number between 1 and 100.")
print("Can you guess what it is?")
while True:
# Ask the player for a guess
player_guess = input("Enter your guess: ")
try:
# Convert the guess to an integer
player_guess = int(player_guess)
except ValueError:
print("Please enter a valid number.")
continue
attempts += 1
# Check the player's guess
if player_guess < number_to_guess:
print("Too low! Try again.")
elif player_guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You've guessed the number in {attempts} attempts.")
break
# Start the game
number_guessing_game()