Tell us what’s happening:
Cannot get this to pass one of the checks. I’ve tried everything. Even running this through Co-Pilot to see if it could pick up where the error is occurring. This is what it is failing: 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. The debug statements show that the correct values are being applied. I’m having a similar issue with this and the salon project (see other post) and so I think something could be amiss with how these are evaluated for things like this.
Your code so far
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
LOGIN () {
echo -e "\nEnter your username:"
read USERNAME
# check if username is in database by pulling id
PLAYER_ID=$($PSQL "SELECT player_id FROM players WHERE username='$USERNAME'")
if [[ -z $PLAYER_ID ]]
then
# set up a new player
INSERT_INTO_PLAYERS=$($PSQL "INSERT INTO players(username,games_played) VALUES('$USERNAME',0)")
# greet
echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."
else
# look up the number of games
GAMES_PLAYED=$($PSQL "SELECT games_played FROM players WHERE username='$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM players WHERE username='$USERNAME'")
echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
}
RUNGUESS () {
if [[ ! $GUESS =~ ^-?[0-9]+$ ]]
then
echo -e "\nThat is not an integer, guess again:"
NUMBER_OF_GUESSES=$(( NUMBER_OF_GUESSES + 1 ))
RUNGAME
elif [[ $GUESS -eq $SECRET_NUMBER ]]
then
UPDATEDB
# Debug statements
echo "DEBUG: NUMBER_OF_GUESSES = $NUMBER_OF_GUESSES"
echo "DEBUG: SECRET_NUMBER = $SECRET_NUMBER"
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
exit
else
if [[ $GUESS -lt $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
else
echo "It's lower than that, guess again:"
fi
read GUESS
NUMBER_OF_GUESSES=$(( NUMBER_OF_GUESSES + 1 ))
RUNGUESS
fi
}
GAME () {
# generate random number
SECRET_NUMBER=$(( (RANDOM % 1000) + 1 ))
# set number of guesses to 1
NUMBER_OF_GUESSES=1
# increase games played
if [[ -z $GAMES_PLAYED ]]
then
GAMES_PLAYED=0
fi
GAMES_PLAYED=$(( GAMES_PLAYED + 1 ))
RUNGAME
}
RUNGAME () {
echo -e "\nGuess the secret number between 1 and 1000:"
read GUESS
RUNGUESS
}
UPDATEDB () {
UPDATEGAMESPLAYED=$($PSQL "UPDATE players SET games_played=$GAMES_PLAYED WHERE player_id=$PLAYER_ID")
if [[ -z $BEST_GAME || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
# update database
INSERT_NEW_BEST=$($PSQL "UPDATE players SET best_game=$NUMBER_OF_GUESSES WHERE player_id=$PLAYER_ID")
fi
}
LOGIN
GAME
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game