Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

The tests won’t pass even though the bash code does what’s asked. Am I doing anything wrong?

Your code so far

#!/bin/bash

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

echo "Enter your username:"
read USERNAME

USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
if [[ $USER_ID ]]
  then
    GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID")
    BEST_GAME=$($PSQL "SELECT guesses FROM games ORDER BY guesses ASC LIMIT 1")
    echo $"Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
  $($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
  echo Welcome, $USERNAME! It looks like this is your first time here.    
fi

play_game(){
  GAME_NUMBER_SECRET=$((1 + RANDOM % 1000))
  echo Guess the secret number between 1 and 1000
  
  NUMBER_OF_GUESSES=0
  while [[ $USER_GUESS != $GAME_NUMBER_SECRET ]]
  do
    read USER_GUESS
    if [[ $USER_GUESS =~ ^[0-9]+$ ]]
      then
        if [[ $USER_GUESS -lt $GAME_NUMBER_SECRET ]]
          then
            echo "It's higher than that, guess again:"
        elif [[ $USER_GUESS -gt $GAME_NUMBER_SECRET ]]
          then
            echo "It's lower than that, guess again:"
        fi
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))
    else
      echo That is not an integer, guess again:
    fi
  done
  echo $"You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GAME_NUMBER_SECRET. Nice job!"
}

play_game

$($PSQL "INSERT INTO games(guesses, user_id) VALUES($NUMBER_OF_GUESSES, $USER_ID)")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

1 Like

I’ve updated my code like so:

#!/bin/bash

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

echo "Enter your username:"
read USERNAME

# Verificar si el usuario existe
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
if [[ $USER_ID ]]
then
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID")
  BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games WHERE user_id = $USER_ID")

  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
  $PSQL "INSERT INTO users(username) VALUES('$USERNAME')"
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
fi

play_game() {
  GAME_NUMBER_SECRET=$((1 + RANDOM % 1000))
  echo "Guess the secret number between 1 and 1000:"

  NUMBER_OF_GUESSES=0
  while true
  do
    read USER_GUESS

    if [[ $USER_GUESS =~ ^[0-9]+$ ]]
    then
      NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

      if [[ $USER_GUESS -lt $GAME_NUMBER_SECRET ]]
      then
        echo "It's higher than that, guess again:"
      elif [[ $USER_GUESS -gt $GAME_NUMBER_SECRET ]]
      then
        echo "It's lower than that, guess again:"
      else
        break
      fi
    else
      echo "That is not an integer, guess again:"
    fi
  done

  echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GAME_NUMBER_SECRET. Nice job!"
}

play_game

$PSQL "INSERT INTO games(guesses, user_id) VALUES($NUMBER_OF_GUESSES, $USER_ID)"

and most of the tests pass. I still can’t the two of them to pass

I managed to fix the problem by storing the queries in variables.
Here’s my code in case anyone runs into the same problem:

#!/bin/bash

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

echo "Enter your username:"
read USERNAME

# Verificar si el usuario existe
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
if [[ $USER_ID ]]
then
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID")
  BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games WHERE user_id = $USER_ID")

  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
  INSERT_USER=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
fi

play_game() {
  GAME_NUMBER_SECRET=$((1 + RANDOM % 1000))
  echo "Guess the secret number between 1 and 1000:"

  NUMBER_OF_GUESSES=0
  while true
  do
    read USER_GUESS

    if [[ $USER_GUESS =~ ^[0-9]+$ ]]
    then
      ((NUMBER_OF_GUESSES += 1))

      if [[ $USER_GUESS -lt $GAME_NUMBER_SECRET ]]
      then
        echo "It's higher than that, guess again:"
      elif [[ $USER_GUESS -gt $GAME_NUMBER_SECRET ]]
      then
        echo "It's lower than that, guess again:"
      else
        break
      fi
    else
      echo "That is not an integer, guess again:"
    fi
  done

  echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GAME_NUMBER_SECRET. Nice job!"
}

play_game

INSERT_GAME=$($PSQL "INSERT INTO games(guesses, user_id) VALUES($NUMBER_OF_GUESSES, $USER_ID)")