Issue with passing tests, Number Guessing Game PostgreSQL

Hello,

I have been having issues with passing the tests of the final certification project for relational databases. I’ve spent a few hours trying to resolve the issues but I haven’t figured it out. There are 2 tests in particular that won’t seem to let my code 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

And

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

This is my code:

#!/bin/bash

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

GUESS()
{
	USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$1';")
	NUMBER=0;
	RANDOM_NUMBER=$(( RANDOM % 1000 + 1 ));
	NUMBER_TRIES=0;

	echo "Guess the secret number between 1 and 1000:"

	until [[ $NUMBER == $RANDOM_NUMBER ]]
	do
		read NUMBER;
		
		if [[ ! $NUMBER =~ ^[0-9]+$ ]]
		then
			echo "That is not an integer, guess again:"
		else
			if [[ $NUMBER -gt $RANDOM_NUMBER ]]
			then
				echo "It's lower than that, guess again:"
			else
				echo "It's higher than that, guess again:"
			fi

			NUMBER_TRIES=$(( NUMBER_TRIES + 1 ))
		fi
	done

	INSERT_GAME_RESULT=$($PSQL "INSERT INTO games (number_of_guesses, user_id) VALUES ($NUMBER_TRIES, $USER_ID);")

	echo "\nYou guessed it in $NUMBER_TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!\n"
}

echo "Enter your username:";
read USER_NAME;

FIND_USER=$($PSQL "SELECT name FROM users WHERE name = '$USER_NAME'");

if [[ -z $FIND_USER ]]
then
	echo -e "\nWelcome, $USER_NAME! It looks like this is your first time here.\n";
	INSERT_NEW_USER_RESULT=$($PSQL "INSERT INTO users (name) VALUES ('$USER_NAME');");
else
	# GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games INNER JOIN users USING(user_id) WHERE name = '$USER_NAME' GROUP BY (user_id)");
	# BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games INNER JOIN users USING(user_id) WHERE name = '$USER_NAME' GROUP BY (user_id)");

	USER_ID=$($PSQL "SELECT user_id FROM users WHERE name = '$USER_NAME'")
	GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id = $USER_ID GROUP BY user_id")
	BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id = $USER_ID GROUP BY user_id")
	USER_NAME_FROM_DB=$($PSQL "SELECT name FROM users WHERE user_id = $USER_ID")

	echo -e "\nWelcome back, $USER_NAME_FROM_DB! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.\n"
fi

GUESS $USER_NAME

I don’t know what I’m doing wrong, it probably has something to do with the echo like including the -e flag and new line characters or something like that but I just don’t know what it is.

I googled a solution for this project (the shell file) and i found this persons solution on github: https://github.com/SarangWadode/fcc-number-guess/blob/main/number_guess.sh#L5

I pasted their work and their solution also didn’t pass. So I guess it has something to do with the tests themselves or this code also doesn’t work.

Can you share a dump of your database using the command in the instructions @SafetImamovic? I will take a look.

Here is the link to the sql file:
https://github.com/SafetImamovic/universe_database/blob/main/NUMBER_GUESS_DB/number_guess.sql

I figured out what was wrong, I guess I needed to store the random number in the games table aswell as the other game information. So I added a column secret_number and then it passed the tests.

Anyone else have luck with this? This solution is not working for me.

Even more oddly, it is randomly swapping to green checks for “If that username has been used before” and “When the secret number is guessed”

Like I’ll run it, “used before” will check green, and “when guessed” will be grey.
Then I’ll run it again, and “when guessed” will turn green and “used before” will go from green to grey.

The output says this for either one or the other:
“AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:”
Always one of them, sometimes both of them between runs, without touching the code, just hitting run.

I’m going insane. It’s been days. I’ve swapped my code around so much, used other solutions, restarted the whole thing, literally nothing will make them both green, it’s just 1 or the other between Runs, and I’m not even changing the code between the runs.

FINAL EDIT: It solved itself. I just kept hitting RUN until it worked facepalm
Hopefully this helps anyone else who comes here with my problem.
JUST KEEP HITTING RUN.

1 Like

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