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.
#!/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
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.
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.
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.