Tests doesn't pass even though the result is right in bash

Im buildind the “number guessing game project” from the relational database course. The problem is that my tests doesn’t pass even though the code is working perfectly fine and giving the expect behaviour.

These are the tests that doesn’t pass:

  • 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

  • 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

Also, sorry for my bad english, it isn’t my main language

This is my .sh file:

#!/bin/bash

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

MAIN(){
echo “Enter your username:”
read USERNAME

CHECK_USER=$($PSQL "SELECT id FROM users WHERE username = '$USERNAME'")

if [[ $CHECK_USER ]]; then
  GAMES_PLAYED=$($PSQL "SELECT COUNT(user_id) FROM games WHERE user_id = $CHECK_USER")
  BEST_GAME=$($PSQL "SELECT MIN(tries) FROM games WHERE user_id = $CHECK_USER")

  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else 
echo "Welcome, $USERNAME! It looks like this is your first time here."
$PSQL "INSERT INTO users(username) VALUES('$USERNAME')"
CHECK_USER=$($PSQL "SELECT id FROM users WHERE username = '$USERNAME'")
fi

MENU
}

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

TRIES=0;
GUESSED=0;

echo -e “Guess the secret number between 1 and 1000:”
while [[ $GUESSED = 0 ]]; do
read GUESS_NUMBER

if [[ ! $GUESS_NUMBER =~ ^[0-9]+$ ]]; then
  echo -e "That is not an integer, guess again:"
  elif [[ $RANDOM_NUMBER = $GUESS_NUMBER ]]; then
    TRIES=$(($TRIES + 1))
    echo -e "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!"

    $PSQL "INSERT INTO games(user_id, tries) VALUES($CHECK_USER, $TRIES)"

    GUESSED=1;
  elif [[ $RANDOM_NUMBER -gt $GUESS_NUMBER ]]; then
    TRIES=$(($TRIES + 1))
    echo -e "It's higher than that, guess again:"
  else
    TRIES=$(($TRIES + 1))
    echo -e "It's lower than that, guess again:"
fi

done
}

MAIN

I just finished this project, and had a lot of problems. I didn’t spend time completely diagnosing cause and effect, but seems it might be pretty picky about \n characters. I was failing a lot, but managed afterward to pass them by putting \n at the beginning of the strings. BUT, I believe there was one string that I had to leave the \n off for some reason, might have been the You Guessed It message, but can’t be sure. I’d start by putting \n in front of whatever strings you’re failing.

Hope that helps. I thought about bringing that up as a bug report or something, but was pressed for time to get it submitted, and then just haven’t gone back to it again.

Unfortunately, my code still doesn’t work even with -e and \n. Anyways, thanks for reaching out.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.