Build a Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

My script will not pass this test no matter what I do:
If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses.This it the output I get : Enter your username:
david
Welcome back, david! You have played 1 games, and your best game took 11 guesses.
Guess the secret number between 1 and 1000: (runs the number guessing, this works fine)
You guessed it in 14 tries. The secret number was 818. Nice job!
this is my script. can anyone help?

#!/bin/bash

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



echo "Enter your username:"

read USERNAME

DATA=$($PSQL "SELECT \* FROM users WHERE name='$USERNAME'")



if \[\[ -z $DATA \]\]; then

$PSQL "INSERT INTO users(name, games, bestscore) VALUES('$USERNAME', 0, 0)"

echo "Welcome, $USERNAME! It looks like this is your first time here."

else

IFS="|" read -r ID USERNAME GAMES_PLAYED BEST_GAME <<< $DATA

echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."

fi



\# Game logic

SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))

NUMBER_OF_GUESSES=0

echo "Guess the secret number between 1 and 1000:"



while true; do

read GUESS

if ! \[\[ $GUESS =\~ ^-?\[0-9\]+$ \]\]; then

echo "That is not an integer, guess again:"

continue

fi

  (( NUMBER_OF_GUESSES++ ))

if \[\[ $GUESS -lt $SECRET_NUMBER \]\]; then

echo "It's higher than that, guess again:"

elif \[\[ $GUESS -gt $SECRET_NUMBER \]\]; then

echo "It's lower than that, guess again:"

else

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

break

fi

done



\# Save results

GAMES_PLAYED=$(( GAMES_PLAYED + 1 ))

if \[\[ -z $DATA \]\]; then

$PSQL "UPDATE users SET bestscore=$NUMBER_OF_GUESSES WHERE name='$USERNAME'"

$PSQL "UPDATE users SET games=$GAMES_PLAYED WHERE name='$USERNAME'"

else

if \[\[ $NUMBER_OF_GUESSES -lt $BEST_GAME \]\] || \[\[ $BEST_GAME -eq 0 \]\]; then

$PSQL "UPDATE users SET bestscore=$NUMBER_OF_GUESSES WHERE name='$USERNAME'"

fi

$PSQL "UPDATE users SET games=$GAMES_PLAYED WHERE name='$USERNAME'"

fi

Your code so far

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/26.2 Safari/605.1.15

Challenge Information:

Build a Number Guessing Game - Build a Number Guessing Game

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

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

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

can you redirect a copy of your output to a file and compare it to the sample output?
There may be some differences you need to account for