Number Guessing Game -- username inserting problem

Here we go…
Hey guys, I’ve got yet another problem. :face_exhaling:
I’ve been trying to finish up the certification project – Number Guessing Game.

I managed to finish up everything except for these two tests: the welcome message when a user logs in again, and the congratulatory message that updates player data. Here’s my code so far:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
NUMBER_OF_GUESSES=0
echo "Enter your username: "
read USERNAME

LOG_IN=$($PSQL "SELECT username FROM players WHERE username = '$USERNAME';")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM players WHERE username = '$USERNAME';")
BEST_GAME=$($PSQL "SELECT best_game FROM players WHERE username = '$USERNAME';")
# if username doesn't exist:
if [[ -z $LOG_IN ]]
then
 echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
 INSERT_USERNAME=$($PSQL "INSERT INTO players(username, best_game, games_played) VALUES('$USERNAME', 0, 0);")
 # otherwise, welcome the existing user
else
 echo -e "Welcome back, $USERNAME! 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:"
NUMBER_TO_GUESS=$(( 1 + RANDOM % 1000 ))
GUESSING_GAME() {
  
  read GUESS
if [[ $GUESS =~ ^[0-9]+$ ]]
then

  if [[ $GUESS -lt $NUMBER_TO_GUESS ]]
  then
   echo "It's higher than that, guess again:"
   ((NUMBER_OF_GUESSES++))
   GUESSING_GAME
  elif [[ $GUESS -gt $NUMBER_TO_GUESS ]]
  then
   echo "It's lower than that, guess again:"
   ((NUMBER_OF_GUESSES++))
   GUESSING_GAME  
  else
   echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER_TO_GUESS. Nice job!"
   # update player data 
   ((GAMES_PLAYED++))
   INSERT_DATA=$($PSQL "UPDATE players SET games_played = $GAMES_PLAYED + 1 WHERE username = '$USERNAME';")
   echo $INSERT_DATA
   if [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
   then
   INSERT_DATA2=$($PSQL "UPDATE players SET best_game = $NUMBER_OF_GUESSES WHERE username = '$USERNAME';")
   echo $INSERT_DATA2
   fi
  fi
else
 echo "That is not an integer, guess again:"
 ((NUMBER_OF_GUESSES++))
 GUESSING_GAME
 fi
}
GUESSING_GAME

I double checked for any spelling errors – none that I could see. Any tips?

Hi, @cybershiloh

I think you might refer to this US:

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.

Can you describe your output after playing the game several times?

Also, can you try to control for several kind of usernames to see if that improve the answer? Try to practice positive and negative tests based on the constraints suggested on the exercise. Apply extremes, to see how it works?

@evaristoc ,
I already have played the game several times. The problem is that I check to see if the number of guesses is less than my best game, and I don’t really think it updates it. It always starts at zero.

Ahh… I see the problem. It’s not updating the player data! This is my code now (which still doesn’t work):

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
NUMBER_OF_GUESSES=0
echo "Enter your username: "
read USERNAME

LOG_IN=$($PSQL "SELECT username FROM players WHERE username = '$USERNAME';")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM players WHERE username = '$USERNAME';")
BEST_GAME=$($PSQL "SELECT best_game FROM players WHERE username = '$USERNAME';")
# if username doesn't exist:
if [[ -z $LOG_IN ]]
then
 echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
 INSERT_USERNAME=$($PSQL "INSERT INTO players(username, best_game, games_played) VALUES('$USERNAME', 0, 1000);")
 # otherwise, welcome the existing user
else
 echo -e "Welcome back, $USERNAME! 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:"
NUMBER_TO_GUESS=$(( 1 + RANDOM % 1000 ))
GUESSING_GAME() {
  
  read GUESS
if [[ $GUESS =~ ^[0-9]+$ ]]
then

  if [[ $GUESS -lt $NUMBER_TO_GUESS ]]
  then
   echo "It's higher than that, guess again:"
   ((NUMBER_OF_GUESSES++))
   GUESSING_GAME
  elif [[ $GUESS -gt $NUMBER_TO_GUESS ]]
  then
   echo "It's lower than that, guess again:"
   ((NUMBER_OF_GUESSES++))
   GUESSING_GAME  
  else
   echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER_TO_GUESS. Nice job!"
   # update player data 
   ((GAMES_PLAYED++))
   INSERT_DATA=$($PSQL "UPDATE players SET games_played = $GAMES_PLAYED + 1 WHERE username = '$USERNAME';")
   echo $INSERT_DATA
   if [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
   then
   INSERT_DATA2=$($PSQL "UPDATE players SET best_game = $NUMBER_OF_GUESSES WHERE username = '$USERNAME';")
   echo $INSERT_DATA2
   fi
  fi
else
 echo "That is not an integer, guess again:"
 ((NUMBER_OF_GUESSES++))
 GUESSING_GAME
 fi
}
GUESSING_GAME

Hi @cybershiloh

Try removing any necessary echo statements.

Happy coding

1 Like

Hi @cybershiloh

Please follow @Teller’s advice (he is more experienced than me with this task).

Can you also verify if your changes were committed? I have found from other problem like yours that that was an issue.

1 Like

Thanks for the advice, @Teller!
Now, I just noticed this, but there’s something at the top which says…

Your script should only ask for input from the user to get the username and to take guesses. Your script should output exactly what is described in the user storied below, and nothing extra. The tests will add users to your database when the script has that ability, feel free to delete those. Some script related user stories may not pass until the script is completely working.
Which I thought was strange. :face_with_raised_eyebrow: Every single feature is working completely except for when you guess the secret number, and you beat your best_game and it’s supposed to enter it into the number_guess database. Here’s my code:

echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER_TO_GUESS. Nice job!"
   # update player data 
   ((GAMES_PLAYED++))
   INSERT_DATA=$($PSQL "UPDATE players SET games_played = $GAMES_PLAYED + 1 WHERE username = '$USERNAME';")
  
   if [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
   then
   INSERT_DATA2=$($PSQL "UPDATE players SET best_game = $NUMBER_OF_GUESSES WHERE username = '$USERNAME';")

As for you, @evaristoc, I did make sure to commit any remaining changes.

Hi @cybershiloh

Try nesting the variables in single quote marks.

Happy coding