Skip to content

Commit

Permalink
Merge pull request #209 from shiveshhhhsingh1723/patch-1
Browse files Browse the repository at this point in the history
Create Guess the number game
  • Loading branch information
Open-Source-you authored Oct 21, 2024
2 parents 77fdfbd + cbd993e commit 3df4fb3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Guess the number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()

0 comments on commit 3df4fb3

Please sign in to comment.