Number_guess game

I cant seem to get this to pass the test :8, :13 no matter what i try is there something I’m missing ?

#!/bin/bash

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

# random number generator
SECRET_NUMBER=$((1 + $RANDOM % 1000))
 echo $SECRET_NUMBER
echo "Enter your username:"
 read PLAYER_NAME

USER_NAME=$($PSQL "SELECT user_name FROM users WHERE user_name = '$PLAYER_NAME'" | xargs)
if [[ -z $USER_NAME ]] 
 then
  echo "Welcome, $PLAYER_NAME! It looks like this is your first time here."
  # add user_name
  $PSQL "INSERT INTO users(user_name) VALUES('$PLAYER_NAME');" > /dev/null
else
  # if user_name exist get previous games
 GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games g INNER JOIN users u ON g.user_id = u.user_id WHERE u.user_name = '$PLAYER_NAME'")
 BEST_GAME=$($PSQL "SELECT MIN(guess) FROM games g INNER JOIN users u ON g.user_id = u.user_id WHERE u.user_name = '$PLAYER_NAME'")

if [ "$GAMES_PLAYED" -eq 1 ]; then
  game_text="game"
else
  game_text="games"
fi

if [ "$BEST_GAME" -eq 1 ]; then
  guess_text="guess"
else
  guess_text="guesses"
fi
  # Returning player welcome!!!!!!!!
 echo -e "\nWelcome back, $PLAYER_NAME! You have played 1 $game_text, and your best game took $BEST_GAME $guess_text.\n"
fi

  # set count variable
  GUESS_COUNT=0

  # users guess function
guess_number() {
  echo "Guess the secret number between 1 and 1000:"
  read PLAYERS_GUESS
  ((GUESS_COUNT++))

if [[ ! $PLAYERS_GUESS =~ ^[0-9]+$ ]] 
  then 
   echo "That is not an integer, guess again:"
   guess_number
   
  # if users guess is lower
elif
  [[ $PLAYERS_GUESS -gt $SECRET_NUMBER ]] 
   then
   echo "It's lower than that, guess again:"
   guess_number
  
  # if users guess is higher
elif 
  [[ $PLAYERS_GUESS -lt $SECRET_NUMBER ]]
  then 
   echo "It's higher than that, guess again:"
   guess_number
  else
  # if users guess is correct
  #update_games
   USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name = '$PLAYER_NAME'")
   $PSQL "INSERT INTO games(guess, user_id) VALUES($GUESS_COUNT, $USER_ID)" > /dev/null
  # final message
  
  echo -e "\nYou guessed it in $GUESS_COUNT tries. The secret number was $(( $GUESS_COUNT - 1 )). Nice job!\n"
 fi
 }
 

guess_number
exit 0

The output has to match exactly the expected output. For eg this echo would not be expected:

I forgot to comment that out before I posted but I’ve tested it without that and still doesn’t pass ?

this type of code would make your output not match the expected output?

Please run your game and compare your output to the expected strings and make sure they match exactly. Do not add any extra checks or code that was not requested.

edit: also I suggest not using recursive code unless you make sure you exit the program after the person guesses correctly. Recursive code may not exit immediately after the correct guess (because the whole call stack has to be emptied first).

yeah i realised i put my exit code out side of the function, but i figured it out :slight_smile: thanks for your help hbar1st.

1 Like