My Guessing Game won't be validated

Hello! I have a problem concerning the Guessing game we have to make for the last project in Relational Database. My script works just fine and I have no problem what so ever to make it run. However it seems like freecodecamp won’t validate the following points:

  1. When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! and finish running

  2. 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

I have tried other scripts from others and it still won’t validate my project. Here is my code, please if you can help me it would be great:

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



echo "Enter your username: "
read USERNAME
RANDOM_NUMBER=$((1 + RANDOM % 1000))
SELECTED_USER=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME'")


#if username not found
if [[ -z $SELECTED_USER ]]
then
  INSERT_USERNAME=$($PSQL "INSERT INTO users (username) VALUES('$USERNAME')")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
else
#if username found
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) 
  FROM games 
  JOIN users ON games.user_id = users.user_id 
  WHERE users.username = '$USERNAME';")

  BEST_GUESS=$($PSQL "SELECT MIN(guesses) FROM games 
  JOIN users ON games.user_id = users.user_id
  WHERE users.username = '$USERNAME';")
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GUESS guesses. "
fi

#GRAB USER ID
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")

TRIES=0
GUESS=0
#echo $USER_ID
#echo "$RANDOM_NUMBER"

until [ $RANDOM_NUMBER == $GUESS ]
do
  echo "Guess the secret number between 1 and 1000:"
  read GUESS

  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
    echo "That is not an integer, guess again:"
  else
    (( TRIES++ ))
    if [[ $GUESS -gt $RANDOM_NUMBER ]]
    then
      echo "It's lower than that, guess again:"
    elif [[ $GUESS -lt $RANDOM_NUMBER ]]
    then       
      echo "It's higher than that, guess again:"
    fi
  fi
done

#insert data
INSERTED_GAME_INFO=$($PSQL "INSERT INTO games (user_id, guesses) VALUES ($USER_ID, $TRIES)")

#if equal to random_number
echo "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job! "
1 Like

I have the exact same problem! Somebody help please.

I don’t know if this helps but I finally passed the tests.

The “Welcome back…”-part worked after I removed the duplicate of “Guess the secret number between 1 and 1000:”. I used a function in my solution and when guessing again it always printed this line again, too. When I fixed this, it worked.

Earlier I tried saving the random number in a secret_number column in the database and printing this variable, because I found this possible solution in another forum post. If this was what did the trick is hard to say. But after I removed the random number echo I had in the script for testing purposes, it accepted the “You guessed it…”-part too.

2 Likes

I’ve got a similar issue with my code failing these two tests, if anyone has any ideas I’d love to hear them too!