Number guessing game task 8 and 13 validation problem

Hi,

I have completed this project, and my code output is as per the user stories’ requirements. Yet when I am running tests, my tasks 8 and 13 fail even though the output is as required.

Below is the screen shot of the tasks

My Code is as below:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --tuples-only -c"
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ))
GUESS=0

# Ask for username
echo "Enter your username: "
read USERNAME

# Check if username exissts
USER_EXISTS=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")
# echo -e "\nUserName: $USERNAME"
# If username exists
if [[ -z $USER_EXISTS ]]

then
  INSERT_USER_DATA=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME');")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
else
  # Get u ser game data
  USERDATA=$($PSQL "SELECT username, COUNT(*), MIN(number_of_guesses) FROM users LEFT JOIN games USING(user_id) WHERE username = '$USERNAME' GROUP BY username;")
  echo "$USERDATA" | while read USER_DB_NAME BAR GAMES_PLAYED BAR BEST_GAME
  do
    echo "Welcome back, $USER_DB_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  done
fi


while true
  # Start the game
do
  echo -e "\nGuess the secret number between 1 and 1000:"
  read USER_GUESS


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

  (( GUESS++ ))
  # if guess is greater than the random number
  if [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    # if guess is less thant the random number
  elif [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
  then
    echo "It's higher than that, guess again:"
  else
  # if the guess is correct
  #  Get user_id
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
  # Add game data to database
  INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (user_id, number_of_guesses) VALUES ($USER_ID, $GUESS);")
  # Display game completion message
  echo "You guessed it in $GUESS tries. The secret number was $RANDOM_NUMBER. Nice job!"
  break
  fi
done

Sample gameplay as below with required output

Enter your username: 
John
Welcome back, John! You have played 1 games, and your best game took 9 guesses.

Guess the secret number between 1 and 1000:
500
It's higher than that, guess again:

Guess the secret number between 1 and 1000:
800
It's higher than that, guess again:

Guess the secret number between 1 and 1000:
900
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
850
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
830
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
829
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
810
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
805
It's lower than that, guess again:

Guess the secret number between 1 and 1000:
802
It's higher than that, guess again:

Guess the secret number between 1 and 1000:
803
You guessed it in 10 tries. The secret number was 803. Nice job!

Even bad non numeric guesses should be counted in the total value of the number of guesses. I also suggest reading old posts on the forum if you need further help.

Hi,

I changed my code to count the guess even if the input is not numeric., yet the test results are the same.

I have referred to some old posts regarding this but have not found any solution. Can you please share the link to any particular post that I should refer.

My Updated code is as below:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --tuples-only -c"
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ))
GUESS=0

# Ask for username
echo "Enter your username: "
read USERNAME

# Check if username exissts
USER_EXISTS=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")
# echo -e "\nUserName: $USERNAME"
# If username exists
if [[ -z $USER_EXISTS ]]

then
  INSERT_USER_DATA=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME');")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
else
  # Get u ser game data
  USERDATA=$($PSQL "SELECT username, COUNT(*), MIN(number_of_guesses) FROM users LEFT JOIN games USING(user_id) WHERE username = '$USERNAME' GROUP BY username;")
  echo "$USERDATA" | while read USER_DB_NAME BAR GAMES_PLAYED BAR BEST_GAME
  do
    echo "Welcome back, $USER_DB_NAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  done
fi


while true
  # Start the game
do
  echo -e "\nGuess the secret number between 1 and 1000:"
  read USER_GUESS

  (( GUESS++ ))

  # If guess is not an integer
  if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
  then
    echo "That is not an integer, guess again:"
  # if guess is greater than the random number
  elif [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    # if guess is less thant the random number
  elif [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
  then
    echo "It's higher than that, guess again:"
  else
  # if the guess is correct
  #  Get user_id
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
  # Add game data to database
  INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (user_id, number_of_guesses) VALUES ($USER_ID, $GUESS);")
  # Display game completion message
  echo "You guessed it in $GUESS tries. The secret number was $RANDOM_NUMBER. Nice job!"
  break
  fi
done

Test result screen grab below:

Regards,
Saptarshi

Try setting the random number to 9 in the code and then I want you to play the game and capture the output to a file. Each time you guess, increment your guess by one. So start from 1 as your first guess and increment to two on your next guess etc till you get to 9. Somewhere in the middle, guess an invalid value. After you are done guessing 9, check that you printed out the total guesses is 10. Then copy your output to a post here (your reply) so we can review it. Also copy the sample output given to you here so we can compare it. Thanks.