Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Guess the number game #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()