Number Guessing Game - Build a Number Guessing Game - "Welcome back" test not passing

Tell us what’s happening:
Although my script has the correct output, it does not pass one of the tests. Namely:

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.

All other tests pass. I spent several hours on the forum and noticed that other students had a similar issue with this particular test. I compared my script to other students’ scripts and still could not identify the source of the error. I tried refactoring my code, but it yielded no results.

It appears that the test does not like the “if else” statement in MAIN() but I can’t figure out why. Any help or suggestions are welcome.

My script

#!/bin/bash 
# Number Guessing Game

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

# generate a random variable with a 1 to 1000 range
NUMBER=$(( RANDOM%1000 +1)) 
#echo $NUMBER

MAIN() {
  echo "Enter your username:"
  read NAME
  # check if USER is in the database
  CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE username='$NAME'")
  # greeting the player
  if [[ ! $CUSTOMER_ID ]]
  then 
    echo "Welcome, $NAME! It looks like this is your first time here."
    INSERT_USER=$($PSQL "INSERT INTO customers(username) VALUES('$NAME')")
    CUSTOMER_ID=$($PSQL "SELECT customer_id FROM customers WHERE username='$NAME'")
    GAMES_PLAYED=0
  else 
    USERNAME=$($PSQL "SELECT username FROM customers WHERE customer_id=$CUSTOMER_ID")
    GAMES_PLAYED=$($PSQL "SELECT games_played FROM customers WHERE customer_id=$CUSTOMER_ID")
    BEST_GAME=$($PSQL "SELECT best_game FROM customers WHERE customer_id=$CUSTOMER_ID")
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  fi
  GUESS_GAME
}

# guessing the number
GUESS_GAME() {
  COUNT=0 # number of games in this round
  echo "Guess the secret number between 1 and 1000:"
  while [[ $NUMBER -ne $USER_GUESS ]]
  do 
    read USER_GUESS
    if [[ $USER_GUESS =~ ([^0-9]+)$ ]]
    then
      echo "That is not an integer, guess again:"
      ((COUNT++))
      ((GAMES_PLAYED++))
    else
      if [[ $NUMBER -lt $USER_GUESS ]]
      then
        echo "It's lower than that, guess again:"
        ((COUNT++))
        ((GAMES_PLAYED++))
      elif [[ $NUMBER -gt $USER_GUESS ]]
      then
        echo "It's higher than that, guess again:"
        ((COUNT++))
        ((GAMES_PLAYED++))
      elif [[ $NUMBER -eq $USER_GUESS ]] 
      then
        ((COUNT++))
        ((GAMES_PLAYED++))
        echo "You guessed it in $COUNT tries. The secret number was $NUMBER. Nice job!"
        # check if best_game exists
        if [[ ! $BEST_GAME ]]
        then
          INSERT_BEST_GAME=$($PSQL "UPDATE customers SET best_game=$COUNT WHERE customer_id=$CUSTOMER_ID")
          INSERT_GAMES_PLAYED=$($PSQL "UPDATE customers SET games_played=$GAMES_PLAYED WHERE customer_id=$CUSTOMER_ID")
        elif [[ $COUNT -lt $BEST_GAME ]]
        then
          INSERT_BEST_GAME=$($PSQL "UPDATE customers SET best_game=$COUNT WHERE customer_id=$CUSTOMER_ID")
          INSERT_GAMES_PLAYED=$($PSQL "UPDATE customers SET games_played=$GAMES_PLAYED WHERE customer_id=$CUSTOMER_ID")
        else
          #INSERT_BEST_GAME=$($PSQL "UPDATE customers SET best_game=$COUNT WHERE customer_id=$CUSTOMER_ID")
          INSERT_GAMES_PLAYED=$($PSQL "UPDATE customers SET games_played=$GAMES_PLAYED WHERE customer_id=$CUSTOMER_ID") 
        fi
      fi
    fi
  done
}

MAIN

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Number Guessing Game - Build a Number Guessing Game

Link to the challenge:

Why are you incrementing games played with every guess, even non number guesses?

1 Like

@eoja thank you for pointing this out to me. Of course, non-number guesses should not be included in the total number of guesses. Moreover, games and guesses are completely separate. In my initial script I assumed they are the same (i.e., 1 game = 1 guess, which is obviously incorrect). A customer can only play one game at any given time, but can make many guesses. It seems so obvious to me now. I can go to sleep feeling accomplished, my script passes all tests now. Thanks again.