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

Update guess_number.py #176

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
32 changes: 20 additions & 12 deletions guess_number.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import random

def guess_number():
number_to_guess = random.randint(1, 100) # Random number between 1 and 100
print("Welcome to the Number Guessing Game!")
lower_bound = int(input("Enter the lower bound of the range: "))
upper_bound = int(input("Enter the upper bound of the range: "))

number_to_guess = random.randint(lower_bound, upper_bound) # Random number within user-defined range
attempts = 0
guessed = False

print("Welcome to the Number Guessing Game!")
print("I have chosen a number between 1 and 100. Can you guess it?")
print(f"I have chosen a number between {lower_bound} and {upper_bound}. Can you guess it?")

while not guessed:
user_guess = int(input("Enter your guess: "))
attempts += 1
try:
user_guess = int(input("Enter your guess: "))
attempts += 1

if user_guess < number_to_guess:
print("Too low! Try again.")
elif user_guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
guessed = True
if user_guess < lower_bound or user_guess > upper_bound:
print(f"Please guess a number between {lower_bound} and {upper_bound}.")
elif user_guess < number_to_guess:
print("Too low! Try again.")
elif user_guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
guessed = True
except ValueError:
print("Invalid input. Please enter a valid integer.")

# Start the game
guess_number()