Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Hello, my code for the course “Number Guessing Game” within the Relational Database curriculum will not pass. I go through the prompts and the output seems correct, but the course will still not pass. Any ideas why?

Prompts below

Your code so far

##

#! /bin/bash

PSQL="psql -X --username=freecodecamp --dbname=number_guess --tuples-only -c"

#Generates a random number

NUMBER=$(( RANDOM % 1000 + 1))

echo $NUMBER


#Function that prompts the user to guess a number and executes other variables.

GUESS1() {

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

CURRENT_GAME=$($PSQL "SELECT count(game_id) FROM games WHERE user_id=$USER_ID")

CURRENT_GAME_PLUS_1=$(( CURRENT_GAME + 1 ))

INSERT_GAME=$($PSQL "INSERT INTO games(game,user_id) VALUES($CURRENT_GAME_PLUS_1, $USER_ID)")

GAME_COUNT=$($PSQL "SELECT MAX(game) FROM games WHERE user_id=$USER_ID")

GAME_ID=$($PSQL "SELECT game_id FROM games WHERE user_id=$USER_ID and game =$GAME_COUNT")

}

#Function that checks if the guess is an integer, is too high, or too low

NUMBER_GUESS_FUNCTION() {

read NUMBER_GUESSED

if [[ ! $NUMBER_GUESSED =~ ^[0-9]+$ ]];

then

GUESS_NOT_INT=$($PSQL "INSERT INTO guesses(guess,game_id) VALUES('$NUMBER_GUESSED', $GAME_ID)")

echo "That is not an integer, guess again:"

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED > $NUMBER ]];

then

GUESS_LOWER=$($PSQL "INSERT INTO guesses(guess,game_id) VALUES('$NUMBER_GUESSED', $GAME_ID)")

echo "It's lower than that, guess again:"

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED < $NUMBER ]];

then

GUESS_HIGHER=$($PSQL "INSERT INTO guesses(guess,game_id) VALUES('$NUMBER_GUESSED', $GAME_ID)")

echo "It's higher than that, guess again:"

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED = $NUMBER ]];

then

GUESS_CORRECT=$($PSQL "INSERT INTO guesses(guess,game_id) VALUES('$NUMBER_GUESSED', $GAME_ID)")

GUESS_COUNT=$($PSQL "SELECT COUNT(guess_id) FROM guesses WHERE game_id=$GAME_ID")

WIN_GUESS_COUNT=$($PSQL "INSERT INTO wins(guess_count,game_id) VALUES($GUESS_COUNT, $GAME_ID)")

echo "You guessed it in $GUESS_COUNT tries. The secret number was $NUMBER. Nice job!"
fi
}

#while loop checks to see if username is less than 22 characters

ok=0

while [[ $ok = 0 ]]

do

USERNAME_QUESTION="Enter your username:"

echo $USERNAME_QUESTION

read USERNAME_INPUT

 if [ ${#USERNAME} -gt 22 ]

 then

echo Too long - 22 characters max

else

 ok=1

  fi

done

#Checks to see if the username already is in the system

USERNAME_DATABASE=$($PSQL "SELECT * FROM username WHERE username = '$USERNAME_INPUT'")

if [[ -z $USERNAME_DATABASE ]];

then

INSERT_USERNAME=$($PSQL "INSERT INTO username(username) VALUES('$USERNAME_INPUT')")

USER_ID=$($PSQL "SELECT user_id FROM username WHERE username = '$USERNAME_INPUT'")

echo "Welcome, $USERNAME_INPUT! It looks like this is your first time here."

else

USER_ID=$($PSQL "SELECT user_id FROM username WHERE username = '$USERNAME_INPUT'")

GAMES_PLAYED=$($PSQL"SELECT COUNT(game_id) FROM games WHERE user_id=$USER_ID")

LOWEST_GUESSES=$($PSQL"SELECT MIN(guess_count) FROM wins FULL JOIN games ON games.game_id = wins.game_id FULL JOIN username ON username.user_id = games.user_id WHERE username= '$USERNAME_INPUT'")

echo "Welcome back, $USERNAME_INPUT! You have played $GAMES_PLAYED games, and your best game took $LOWEST_GUESSES guesses."

fi

#Executes the functions from above.

GUESS1

NUMBER_GUESS_FUNCTION


#Repeates the NUMBER_GUESS_FUNCTION until the correct number is guessed

until [[ $NUMBER_GUESSED = $NUMBER ]]

do

NUMBER_GUESS_FUNCTION

done

##

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Hi @codecampacct

Try removing the whitespace from the number of games and number of guessing displayed in the output.

Happy coding

Hello, thank you for responding. Any idea why the white space appears? The code does not have the spacing.

Check the PSQL variable.

Thank you for looking at this with me. I put double quotes around the variables and removed the front space, which seemed to fix it. The course is still not passing though.

The tests maybe timing out.

You don’t need to store guess attempts in a table.

I modified the code to increment guess attempts without using a table and the code passed. Thank you for the help.

1 Like