Number Guessing SQL/bash project

I’ve got a problem but I have no idea why, the test keeps failing even though I have the right expected output.

Here is my code:

#!/bin/bash

# Function to execute psql with predefined options
number_guess_psql() {
  psql --username=freecodecamp --dbname=number_guess -t --no-align -c "$@"
}

# Function to prompt user for a username
get_username() {
  echo "Enter your username:"
  read username
}

# Function to check if a username exists in the database and add new users
check_username() {
  username=$1
  # Check if the username exists in the database
  result=$(number_guess_psql "SELECT COUNT(*) FROM users_games WHERE username = '$username'")
  if [[ ! -z $result && $result -gt 0 ]]; then
    # Get the user's best guess count and games played
    best_game=$(number_guess_psql "SELECT best_game FROM users_games WHERE username = '$username'")
    games_played=$(number_guess_psql "SELECT games_played FROM users_games WHERE username = '$username'")
    echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
  else
    # Add the new user to the database
    number_guess_psql "INSERT INTO users_games (username, best_game, games_played) VALUES ('$username', 0, 0)"
    echo "Welcome, $username! It looks like this is your first time here."
  fi
}

# Function to generate a random number between 1 and 1000
generate_secret_number() {
  echo $((1 + RANDOM % 1000))
}

# Function to prompt user for guess and check if it's correct
guess_number() {
  local secret_number=$1
  local number_of_guesses=0
  local guessed=0
  echo "Guess the secret number between 1 and 1000:"
    while [[ $guessed = 0 ]]; do
    read guess
    # Check if the input is an integer
    if [[ ! $guess =~ ^[0-9]+$ ]]; then
      echo "That is not an integer, guess again:"
      continue
    fi
    if [[ $guess -eq $secret_number ]]; then
      ((number_of_guesses++))
      # Update user's best guess count and games played
      current_best_guess=$(number_guess_psql "SELECT best_game FROM users_games WHERE username = '$username'")
      if [[ $current_best_guess -eq 0 || $current_best_guess -gt $number_of_guesses ]]; then
        number_guess_psql "UPDATE users_games SET best_game = $number_of_guesses WHERE username = '$username'"
      fi
      number_guess_psql "UPDATE users_games SET games_played = COALESCE(games_played, 0) + 1 WHERE username = '$username'"
      echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
      guessed=1
    elif [[ $guess -lt $secret_number ]]; then
      ((number_of_guesses++))
      echo "It's higher than that, guess again:"
    else
      ((number_of_guesses++))
      echo "It's lower than that, guess again:"
    fi
  done
}

# Main script
echo "Welcome to the Number Guessing Game!"
get_username
check_username "$username"
# Generate a random secret number
secret_number=$(generate_secret_number)
# Start the guessing game
guess_number "$secret_number"

edit: that’s my current code:

#!/bin/bash

# Function to execute psql with predefined options
number_guess_psql() {
  psql --username=freecodecamp --dbname=number_guess -t --no-align -c "$@"
}

# Function to prompt user for a username
get_username() {
  echo "Enter your username:"
  read username
}

# Function to check if a username exists in the database and add new users
check_username() {
  username=$1
  # Check if the username exists in the database
  result=$(number_guess_psql "SELECT COUNT(*) FROM users_games WHERE username = '$username'")
  if [[ -z $result ]]; then
    # Get the user's best guess count and games played
    best_game=$(number_guess_psql "SELECT best_game FROM users_games WHERE username = '$username'")
    games_played=$(number_guess_psql "SELECT games_played FROM users_games WHERE username = '$username'")
    echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
  else
    # Add the new user to the database
    number_guess_psql "INSERT INTO users_games (username, best_game, games_played) VALUES ('$username', 0, 0)"
    echo "Welcome, $username! It looks like this is your first time here."
  fi
}

# Function to generate a random number between 1 and 1000
generate_secret_number() {
  echo $((1 + RANDOM % 1000))
}

# Function to prompt user for guess and check if it's correct
guess_number() {
  local secret_number=$1
  local number_of_guesses=0
  local guessed=0
  echo "Guess the secret number between 1 and 1000:"
    while [[ $guessed = 0 ]]; do
    read guess
    # Check if the input is an integer
    if [[ ! $guess =~ ^[0-9]+$ ]]; then
      echo "That is not an integer, guess again:"
      continue
    fi
    if [[ $guess -eq $secret_number ]]; then
      ((number_of_guesses++))
      # Update user's best guess count and games played
      current_best_guess=$(number_guess_psql "SELECT best_game FROM users_games WHERE username = '$username'")
      if [[ $current_best_guess -eq 0 || $current_best_guess -gt $number_of_guesses ]]; then
        number_guess_psql "UPDATE users_games SET best_game = $number_of_guesses WHERE username = '$username'"
      fi
      number_guess_psql "UPDATE users_games SET games_played = COALESCE(games_played, 0) + 1 WHERE username = '$username'"
      echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
      guessed=1
    elif [[ $guess -lt $secret_number ]]; then
      ((number_of_guesses++))
      echo "It's higher than that, guess again:"
    else
      ((number_of_guesses++))
      echo "It's lower than that, guess again:"
    fi
  done
}

# Main script
echo "Welcome to the Number Guessing Game!"
get_username
check_username "$username"
# Generate a random secret number
secret_number=$(generate_secret_number)
# Start the guessing game
guess_number "$secret_number"

sorry for editing the code multiple times I will stay for this as it is the most stable the first had many bugs

Edit:
I refactored the database and the code too
but still no hope with this:

If you share a dump of your database and your current script, I can try and take a look @ebrahemsaleh14.

Edit: I took a look at another person’s project who was having a similar issue - here’s what I found:

try removing the printing of the query responses in the script. e.g. when the script prints INSERT 0 1 or UPDATE 1, etc. That got everything to pass for me.

1 Like

Sure I will check it out
thanks for replying

It worked thanx a lot