Skip to content
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
30 changes: 30 additions & 0 deletions number_guessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import random

def guessing_game():
print("Welcome to the Number Guessing Game!")

upper_bound = int(input("Write the upper bound of the range: "))

print(f"I'm thinking of a number between 1 and {upper_bound}.")

number_to_guess = random.randint(1, upper_bound)
attempts = 0
guessed_correctly = False

while not guessed_correctly:
try:
guess = int(input("Make a guess: "))
attempts += 1

if guess < number_to_guess:
print("Too low.")
elif guess > number_to_guess:
print("Too high.")
else:
guessed_correctly = True
print(f"Congratulations! You've guessed the number {number_to_guess} in {attempts} attempts.")
except ValueError:
print("Please enter a valid integer.")

if __name__ == "__main__":
guessing_game()