Build a Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

this test wont pass though the output is correct: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., with 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

#!/bin/bash

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

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

get_valid_guess() {
  read USERS_GUESS
  while [[ ! $USERS_GUESS =~ ^[0-9]+$ ]]
  do
    echo "That is not an integer, guess again:"
    read USERS_GUESS
  done
}
echo -e "\nEnter your username:\n"
read USER_NAME

USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name = '$USER_NAME'")

if [[ -z $USER_ID ]]
then
echo "Welcome, $USER_NAME! It looks like this is your first time here."
$PSQL "INSERT INTO users(user_name) VALUES('$USER_NAME')"
USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name='$USER_NAME'")
else
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id = $USER_ID")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id = $USER_ID")

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

fi
$PSQL "UPDATE users SET games_played = games_played + 1 WHERE user_id = $USER_ID"
echo -e "\nGuess the secret number between 1 and 1000:\n"
get_valid_guess

NUMBER_OF_GUESSES=1
while [[ $USERS_GUESS -ne $RANDOM_NUMBER ]]
do
  if [[ $USERS_GUESS -gt $RANDOM_NUMBER ]]
  then
    echo "It's lower than that, guess again:"
  else
    echo "It's higher than that, guess again:"
  fi

  get_valid_guess
  NUMBER_OF_GUESSES=$(( NUMBER_OF_GUESSES + 1 ))
done
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $RANDOM_NUMBER. Nice job!"
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id=$USER_ID")

if [[ -z $BEST_GAME || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
  $PSQL "UPDATE users SET best_game=$NUMBER_OF_GUESSES WHERE user_id=$USER_ID"
fi

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Number Guessing Game - Build a Number Guessing Game

1 Like

this is my output:

Welcome back, blue! You have played 2 games, and your best game took 8 guesses.

try counting the guesses that are invalid also.
And confirm that your output doesn’t have any extra unexpected messages from the db commands and that it makes the same text that is expected from the exercise.

invalid guesses like non-integer?

are we counting non-interger guesses too?cuz if not then i think everything is ok

my full output

Enter your username:

blue
Welcome back, blue! You have played 3 games, and your best game took 11 guesses.
UPDATE 1

Guess the secret number between 1 and 1000:

600
It's lower than that, guess again:
500
It's lower than that, guess again:
400
It's lower than that, guess again:
300
It's higher than that, guess again:
350
It's higher than that, guess again:
360
It's higher than that, guess again:
370
It's higher than that, guess again:
380
It's higher than that, guess again:
390 
It's higher than that, guess again:
395
It's lower than that, guess again:
392
It's higher than that, guess again:
394
You guessed it in 12 tries. The secret number was 394. Nice job!

you have some unexpected output.

Like extra “UPDATE 1” being printed.

ohh i lost the exercise showing how to remove that so i dont know how to

what’s the difference between the two lines here?
This may account for the extra output you are seeing.

1 Like

ohh thank you alot that was simple