Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Why two test failed? Number 8 and number 13, no matter what I try. Very annoying…

Your code so far

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

#   ASK FOR THE NAME
echo Enter your username:
read USERNAME
PLAYER=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
#   IF THE NAME IS NEW
if [[ ! -z $PLAYER ]]
then
  #   WELCOME THE USER
  GAMES=$($PSQL "SELECT COUNT(games_id) FROM games WHERE user_id=$PLAYER")
  BEST_GAME=$($PSQL "SELECT MIN(try_number) FROM games WHERE user_id=$PLAYER")
  echo Welcome back, $USERNAME! You have played $GAMES games, and your best game took $BEST_GAME guesses.
else
  #   CREATE A NEW USER
  NEW_PLAYER=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
  PLAYER=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
  echo Welcome, $USERNAME! It looks like this is your first time here.
fi
#   START THE GAME
NUMERO=$(( RANDOM % 1000 + 1 ))
#   RECORD THE SECRET NUMBER IN THE GAME
NEW_GAME=$($PSQL "INSERT INTO games(user_id, secret_number, try_number) VALUES($PLAYER, $NUMERO, 0)")
NEW_GAME=$($PSQL "SELECT games_id FROM games ORDER BY games_id DESC LIMIT 1")
#   GET THE INPUT
echo Guess the secret number between 1 and 1000:
read THE_NUMBER
NUMBER_TRY=1
until [[ $THE_NUMBER -eq $NUMERO ]]
do
  UPDATE=$PSQL "UPDATE games SET try_number=$NUMBER_TRY WHERE games_id=$NEW_GAME"
  if [[ ! $THE_NUMBER =~ ^[0-9]+$ ]]
  then
    echo That is not an integer, guess again:
    read THE_NUMBER
  #   IF THE INPUT IS LOWER THAN THE NUMBER
  elif [[ $THE_NUMBER -gt $NUMERO ]]
  then
    echo It\'s lower than that, guess again:
    read THE_NUMBER
    NUMBER_TRY=$((NUMBER_TRY + 1))
  #   IF THE INPUT IS HIGHER THAN THE NUMBER
  elif [[ $THE_NUMBER -lt $NUMERO ]]
  then
    echo It\'s higher than that, guess again:
    read THE_NUMBER
    NUMBER_TRY=$((NUMBER_TRY + 1))
  fi
done
$PSQL "UPDATE games SET try_number=$NUMBER_TRY WHERE games_id=$NEW_GAME"
echo You guessed it in $NUMBER_TRY tries. The secret number was $NUMERO. Nice job!

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

My fault, sorry. I’m very frustrated :weary:

Hello, I am having the same issue which is not passing the test8. Have you figured out the issue?

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.