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

I added the game 'guess the number' #275

Open
wants to merge 1 commit into
base: master
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
I added the 'guess the number' game
  • Loading branch information
TomerDahari committed Oct 14, 2024
commit 5505bf97225158ffcdc5995f75c4dfb9893e7031
37 changes: 37 additions & 0 deletions guess_the_number/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import random

def guess_game():
print("Welcome to 'Guess the Number' game!")

# Setting the maximum number of attempts
max_attempts = 5
attempts = 0

# The computer chooses a random number between 1 and 100
number_to_guess = random.randint(1, 100)

# The guessing game
while attempts < max_attempts:
try:
# A request from the player to guess a number
guess = int(input(f"Attempt {attempts + 1}/{max_attempts}: Enter a number between 1 and 100: "))

# Checking whether the guess is close to the selected number
if guess < number_to_guess:
print("Too low!")
elif guess > number_to_guess:
print("Too high!")
else:
print(f"Congratulations! You guessed the number {number_to_guess} correctly!")
break

attempts += 1

except ValueError:
print("Invalid input! Please enter a valid number.")

if attempts == max_attempts and guess != number_to_guess:
print(f"Sorry, you've used all {max_attempts} attempts. The correct number was {number_to_guess}.")

if __name__ == "__main__":
guess_game()