Number Guessing Game - Welcome back message problem

Hi everyone,
I manage to pass all the tasks except one:
If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses. , with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game

Can anyone help me with this? I tried to run the game and everything including the output is correct and there is no error or problem.
Thank you so much!
Here is my code:

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

INPUT_NAME(){
  echo "Enter your username:"
  read NAME

  if [[ ${#NAME} -gt 22 || -z $NAME ]]
  then 
    INPUT_NAME
    return
  fi

  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$NAME'")
  
  if [[ -z $USER_ID ]]
  then
    INSERT_NEW_USER=$($PSQL "INSERT INTO users(username) VALUES('$NAME')")
    USERNAME=$($PSQL "SELECT username FROM users WHERE username = '$NAME'" | sed 's/^ *//;s/ *$//')
    echo "Welcome, $USERNAME! It looks like this is your first time here."

  else
    USERNAME=$($PSQL "SELECT username FROM users WHERE username = '$NAME'" | sed 's/^ *//;s/ *$//')
    GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id = $USER_ID" | sed 's/^ *//;s/ *$//')
    BEST_GAME=$($PSQL "SELECT MIN(number_of_guess) FROM games WHERE user_id = $USER_ID" | sed 's/^ *//;s/ *$//')
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  fi

  SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
  NUMBER_OF_GUESS=0
  INPUT_AND_CHECK_GUESSES $USER_ID $SECRET_NUMBER $NUMBER_OF_GUESS
}

INPUT_AND_CHECK_GUESSES(){
  USER_ID=$1
  SECRET_NUMBER=$2
  NUMBER_OF_GUESS=$3

  echo "Guess the secret number between 1 and 1000:"
  while true
  do
    read USER_GUESS

    if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
    then
      echo "That is not an integer, guess again:"
      continue
    fi

    NUMBER_OF_GUESS=$(( $NUMBER_OF_GUESS + 1 ))

    if [[ $USER_GUESS -lt $SECRET_NUMBER ]]
    then
      echo "It's higher than that, guess again:"

    elif [[ $USER_GUESS -gt $SECRET_NUMBER ]]
    then
      echo "It's lower than that, guess again:"
      
    else 
      SAVE_USER_DATA $USER_ID $NUMBER_OF_GUESS 
      echo "You guessed it in $NUMBER_OF_GUESS tries. The secret number was $SECRET_NUMBER. Nice job!"
      break
    fi
  done
}

SAVE_USER_DATA(){
  USER_ID=$1
  NUMBER_OF_GUESS=$2

  INSERT_DATA=$($PSQL "INSERT INTO games(user_id, number_of_guess) VALUES($USER_ID, $NUMBER_OF_GUESS)")
}

INPUT_NAME

Welcome to the forum @Zimy

Either try removing the hash, or commenting out the if condition.

Happy coding

Hi @Teller

I try both of your suggestion separately and it’s still not working :frowning:
I’m so confused about this question

Please share your updated code.

I only change the first part as Teller suggested:

INPUT_NAME(){
  echo "Enter your username:"
  read NAME

  if [[ $NAME -gt 22 || -z $NAME ]]
  then 
    INPUT_NAME
    return
  fi

  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$NAME'")

  # If the username has not been used before
  if [[ -z $USER_ID ]]
  then
    INSERT_NEW_USER=$($PSQL "INSERT INTO users(username) VALUES('$NAME')")
    USERNAME=$($PSQL "SELECT username FROM users WHERE username = '$NAME'" | sed 's/^ *//;s/ *$//')
    echo "Welcome, $USERNAME! It looks like this is your first time here."

  # If that username has been used before
  else
    USERNAME=$($PSQL "SELECT username FROM users WHERE username = '$NAME'" | sed 's/^ *//;s/ *$//')
    GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id = $USER_ID" | sed 's/^ *//;s/ *$//')
    BEST_GAME=$($PSQL "SELECT MIN(number_of_guess) FROM games WHERE user_id = $USER_ID" | sed 's/^ *//;s/ *$//')
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  fi

  # Random number generator
  SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
  NUMBER_OF_GUESS=0
  INPUT_AND_CHECK_GUESSES $USER_ID $SECRET_NUMBER $NUMBER_OF_GUESS
}

So, I haven’t done this one yet, but seems strange that all of your code is with an INPUT_NAME() function that is never called outside of the function.

Maybe I’m missing something…