Hello, I’m trying to do the final project of the certification but I can’t get through a condition. I don’t know why it keeps failing I tested with multiple usernames and I can correctly take number of guesses, compare them to the best guess and update the table as needed. I’d be glad if you could help.
Here is my .sh file:
#!/bin/bash
PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -q -c”
GAME_BEGINS(){
#ask for input "Enter your username: " varchar 22
echo "Enter your username: "
read USERNAME
DOES_NAME_EXIST=$($PSQL “SELECT username FROM users WHERE username = ‘$USERNAME’”)
if \[\[ -z $DOES_NAME_EXIST \]\]
then
#if its new username, "Welcome, !
echo "Welcome, $USERNAME! It looks like this is your first time here."
$PSQL "INSERT INTO users(username, games_played) VALUES('$USERNAME', 0)"
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username = '$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME'")
else
#if its used before, echo "Welcome back,
GAMES_PLAYED=$($PSQL “SELECT games_played FROM users WHERE username = ‘$USERNAME’”)
BEST_GAME=$($PSQL “SELECT best_game FROM users WHERE username = ‘$USERNAME’”)
echo “Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.”
fi
#a variable to track user tries
ATTEMPT=0
#next, game begins. echo "Guess the secret
##number between 1 and 1000: " and read INPUT
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
echo “Guess the secret number between 1 and 1000:”
while true
do
read USER_GUESS
#if input != integer, ask for input again
if \[\[ ! $USER_GUESS =\~ ^\[0-9\]+$ \]\]
then
echo "That is not an integer, guess again:"
continue
fi
#incease attempt count if the input is valid
(( ATTEMPT++ ))
#if input > secret_number
if \[\[ $USER_GUESS -gt $SECRET_NUMBER \]\]
then
echo "It's lower than that, guess again:"
#if input < secret_number
elif \[\[ $USER_GUESS -lt $SECRET_NUMBER \]\]
then
echo “It’s higher than that, guess again:”
else
#game should be won
echo "You guessed it in $ATTEMPT tries. The secret number was $SECRET_NUMBER. Nice job!"
#if best_game doesnt exist or is higher than attempt, update it
if \[\[ -z $BEST_GAME || $ATTEMPT -lt $BEST_GAME \]\]
then
$PSQL "UPDATE users SET best_game = $ATTEMPT WHERE username = '$USERNAME'"
fi
#increase game_played count, its set to 0 above so there wont be null errors for new users
if \[\[ -z $GAMES_PLAYED \]\]
then
$PSQL "UPDATE users SET games_played = 1 WHERE username = '$USERNAME'"
else
$PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$USERNAME'"
fi
break
fi
done
}
GAME_BEGINS
And this is how my table looks like:
