Number Guessing Game - Build a Number Guessing Game

Hey, i can’t get the test “If that username has been used before …” to work and i don’t know why.

Everything works fine when i run the script.

Here is the link to the guthub repo if you want to check the database too:

Thank you all !

#!/bin/bash

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

CREATE_USER() {
$PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 0, 0)"
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
}

FIND_USER() {
USER=$($PSQL "SELECT username FROM users WHERE username='$USERNAME'")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username='$USERNAME'")
}

PLAY_GAME() {
SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
NUMBER_OF_GUESSES=1

echo -e "\nEnter your username:"
read USERNAME
FIND_USER
#if no user create one
if [[ -z $USER ]]
  then
  CREATE_USER
  else
  echo -e "\nWelcome back, $USER ! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

echo $SECRET_NUMBER
echo -e "\nGuess the secret number between 1 and 1000:"
read GUESS

#add a games_played + 1
$PSQL "UPDATE users SET games_played = games_played + 1 WHERE username='$USERNAME'"

while [[ $GUESS -ne $SECRET_NUMBER ]]
do
  if [[ $GUESS != [0-9]* ]]
    then
    echo "That is not an integer, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -lt $SECRET_NUMBER ]]
    then 
    echo "It's higher than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -gt $SECRET_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))
    fi
done

if [[ $BEST_GAME -eq 0 ]] || [[ $BEST_GAME -gt $NUMBER_OF_GUESSES ]]
  then
  $PSQL "UPDATE users SET best_game = $NUMBER_OF_GUESSES WHERE username='$USERNAME'"
fi

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

PLAY_GAME

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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:

I’m seeing couple print outs that shouldn’t really be there.

Enter your username:
me

Welcome back, me ! You have played 1 games, and your best game took 4 guesses.
961

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

Line similar to UPDATE 1 is also displayed in couple other occasions, that’s affecting the tests, similar is with the debug output of secret number. There’s also whitespace before the !, and after the username in the returning user line.

Hey, thank you for your feedback.

I fixed that but the result is the same.

New code here :

#!/bin/bash

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

CREATE_USER() {
$PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 0, 0)"
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
}

FIND_USER() {
USER=$($PSQL "SELECT username FROM users WHERE username='$USERNAME'")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username='$USERNAME'")
}

PLAY_GAME() {
SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
NUMBER_OF_GUESSES=1

echo -e "\nEnter your username:"
read USERNAME
FIND_USER
#if no user create one
if [[ -z $USER ]]
  then
  CREATE_USER
  else
  echo -e "\nWelcome back, $USER! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

echo -e "\nGuess the secret number between 1 and 1000:"
read GUESS

#add a games_played + 1
UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE username='$USERNAME'")

while [[ $GUESS -ne $SECRET_NUMBER ]]
do
  if [[ $GUESS != [0-9]* ]]
    then
    echo "That is not an integer, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -lt $SECRET_NUMBER ]]
    then 
    echo "It's higher than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -gt $SECRET_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))
    fi
done

if [[ $BEST_GAME -eq 0 ]] || [[ $BEST_GAME -gt $NUMBER_OF_GUESSES ]]
  then
  $PSQL "UPDATE users SET best_game = $NUMBER_OF_GUESSES WHERE username='$USERNAME'"
fi

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

PLAY_GAME

These are still printed

UPDATE 1

Yep you were right, i needed to put the “update” into a variable.

Here is the code which is working :

#!/bin/bash

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

CREATE_USER() {
CREATE_USER_RESULT=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 0, 0)")
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
}

FIND_USER() {
USER=$($PSQL "SELECT username FROM users WHERE username='$USERNAME'")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username='$USERNAME'")
}

PLAY_GAME() {
SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
NUMBER_OF_GUESSES=1

echo -e "\nEnter your username:"
read USERNAME
FIND_USER
#if no user create one
if [[ -z $USER ]]
  then
  CREATE_USER
  else
  echo -e "\nWelcome back, $USER! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

echo -e "\nGuess the secret number between 1 and 1000:"
read GUESS

#add a games_played + 1
UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE username='$USERNAME'")

while [[ $GUESS -ne $SECRET_NUMBER ]]
do
  if [[ $GUESS != [0-9]* ]]
    then
    echo "That is not an integer, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -lt $SECRET_NUMBER ]]
    then 
    echo "It's higher than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))

  elif [[ $GUESS -gt $SECRET_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
    read GUESS
    ((NUMBER_OF_GUESSES++))
    fi
done

if [[ $BEST_GAME -eq 0 ]] || [[ $BEST_GAME -gt $NUMBER_OF_GUESSES ]]
  then
  UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET best_game = $NUMBER_OF_GUESSES WHERE username='$USERNAME'")
fi

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

PLAY_GAME

Thanks a lot bro’.

It’s frustrating when those little bugs pop up, isn’t it? I’ve been there too. Sometimes it’s like everything runs smoothly until you hit that one test! Have you tried debugging it step by step? As for the database, I’ll take a look and see if there’s anything that jumps out. And hey, if you’re ever up for a break, there are always those games that pay real money instantly to unwind with!