import random

def guess_number(lower_bound, upper_bound, attempts):
  """
  Starts the number guessing game with given parameters.

  Args:
    lower_bound: The lower limit of the random number range.
    upper_bound: The upper limit of the random number range.
    attempts: The number of allowed guesses.
  """

  # Generate the secret number
  secret_number = random.randint(lower_bound, upper_bound)

  # Start the game loop
  for i in range(attempts):
    guess = int(input(f"Guess a number between {lower_bound} and {upper_bound} (Attempts left: {attempts - i}): "))

    if guess == secret_number:
      print("Congratulations! You guessed the number in", i + 1, "attempts.")
      return True
    elif guess < secret_number:
      print("Your guess is too low.")
    else:
      print("Your guess is too high.")

  # Game over
  print(f"Sorry, you ran out of attempts. The number was {secret_number}.")
  return False

# Set game parameters
lower_bound = 1
upper_bound = 100
attempts = 5

# Play the game
play_again = True
while play_again:
  result = guess_number(lower_bound, upper_bound, attempts)

  # Ask if the user wants to play again
  play_again = input("Do you want to play again? (y/n): ").lower() == "y"