Number Guessing Game - Tests Failing but code is correct

Hi Guys,

I have been trying to complete the last project in the course “Relational Database”. I have completed my code and the output meets all the requirements. But still, when I run the tests, two of the sub-tasks are showing incomplete.

Below are the sub-task details:

Sub-Task 1
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

Sub-Task 2
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

All other sub-tasks have passed the test.

My code is outputting the same text as required for the sub-task yet I am unable to pass the test.

Below is my GitPod URL:
https://gitpod.io/start/#freecodecam-learnnumber-oo61x193pmc

Welcome back to the forum @saptarshi.majumdar

The link is not working for me.

Can you post screen grabs of your output?

Happy coding

Hi,

Attaching the grab of outputs below

My Code As below

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --tuples-only -c"

# ASK FOR USERNAME
echo -e "\nEnter your username:"
read USERNAME

# CHECK IF USERNAME EXISTS
USER_EXISTS=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")

# IF USER DOESNOT EXIST, CREATE USER
if [[ -z $USER_EXISTS ]] 
then
  # CREATE USER IN DATABASE
  USER_CREATE_RESULT=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME');")

  # WELCOME MESSAGE TO USER
  echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
else
  # GET USER STATS FROM DATABASE
  # USER_STATS=$($PSQL "SELECT  username, COUNT(game_id) AS games_played, MIN(number_of_guesses) AS best_game  FROM users INNER JOIN games
  # USING(user_id) WHERE username = '$USERNAME' GROUP BY username;")

  # # ASSIGN THE VALUES TO VARIABLES
  # echo "$USER_STATS" | while read NAME BAR GAMES_PLAYED BAR BEST_GAME
  # do
  #   echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  # done

  USER_NAME=$($PSQL "SELECT username FROM users where username = '$USERNAME';")
  GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM users LEFT JOIN games USING(user_id) WHERE username = '$USERNAME';")
  BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM users LEFT JOIN games USING(user_id) WHERE username = '$USERNAME';")

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

# GENERATE RANDOM NUMBER
RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ))
GUESS=0

# echo "Random No: $RANDOM_NUMBER"

while true 
do
  # START THE GAME
  echo -e "\nGuess the secret number between 1 and 1000:"
  read USER_GUESS

  # CHECK IF INPUT IS NOT AN INTEGER
  if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
  then
    echo -e "\nThat is not an integer, guess again:"
    continue
  fi

  # INCREMENT THE GUESS BY 1
  (( GUESS++ ))

  # IF USER GUESS IS GREATER THAN THE SECRET NUMBER
  if [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
  then
    # TELL USER TO GUESS AGAIN
    echo -e "\nIt's higher than that, guess again:"

  elif [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
  then
    # TELL USER TO GUESS AGAIN
    echo -e "\nIt's lower than that, guess again:"
  else
    # GET USER ID
    USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
    # ADD GAME RESULT TO DATABASE
    INSERT_GAME_RESULT=$($PSQL "InSERT INTO games(user_id, number_of_guesses) VALUES($USER_ID, $GUESS);")
    # DISPLAY GAME COMPLETION MESSAGE
    echo -e "\nYou guessed it in $GUESS tries. The secret number was $RANDOM_NUMBER. Nice job!"
    break
  fi
done

Hi @saptarshi.majumdar

It looks like you are inserting rows into the table with the game results, instead of updating the table.

What does your table look like after each game?

Happy coding

Hi,

Here is a screen grab of the data stored in the database.

Hi @saptarshi.majumdar

You only need one table for this project.
You only need to save the least number of guesses.

Happy coding

Hi,

I don’t think the issue is whit the number of databases. The tests that are not passing are for the output messages. First in case of an existing user and second on winning.
My code output is as required by the test, yet it is failing.

Regards,
Saptarshi

Hi @saptarshi.majumdar

username rishi has played four games, however the output is returning six games.

How are you storing the number of games? Also, if the table is not efficiently organised, the tests will time out and fail.

Hi,

The number of games played is just a count of the games from the games table where the user is ‘Rishi’.

I’ll go through my code once more and compare the output with SQL query.

Regards,
Saptarshi

Hi,

I have gone through the code and compared the output of the code with the SQL query. The code output is showing correct data. Attaching a screenshot of the same.

Regards,
Saptarshi

Hi @saptarshi.majumdar

You are using a long query to get that information.

Consider this, if four players each played twenty five games, how many entries will your table create?

I don’t know how the tests run, but maybe they play several games with different usernames. Then check if the information returned is correct. If it takes too long to process that information, then the tests will fail.

So even though the correct information is returned by the script, the tests still fail.