As many others, I also have a problem with unit test #8 and am trying to figure out where my mistake is.
My unit test #8
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
is failing.
Surprisingly, the fourth-last test
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
is only passing if i exit my script before the insert statement (which should be wrong in my case).
Could anyone help me out? Thank you very much.
These are my relations for games
Table "public.games"
Column | Type | Collation | Nullable | Default
-------------------+-----------------------------+-----------+----------+----------------------------------------
game_id | integer | | not null | nextval('games_game_id_seq'::regclass)
user_id | integer | | |
number_of_guesses | integer | | not null |
played_on | timestamp without time zone | | | CURRENT_TIMESTAMP
Indexes:
"games_pkey" PRIMARY KEY, btree (game_id)
Foreign-key constraints:
"games_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(user_id)
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guessing_game -t --no-align -c"
echo "Enter your username:"
read USERNAME
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME';")
if [[ -z $USER_ID ]]; then
$PSQL "INSERT INTO users(username) VALUES('$USERNAME');"
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME';")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id=$USER_ID;")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id=$USER_ID;")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
NUMBER_OF_GUESSES=0
echo "Guess the secret number between 1 and 1000: "
while true; do
read GUESS
if ! [[ "$GUESS" =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
continue
fi
((NUMBER_OF_GUESSES++))
if [[ $GUESS -lt $SECRET_NUMBER ]]; then
echo "It's higher than that, guess again:"
elif [[ $GUESS -gt $SECRET_NUMBER ]]; then
echo "It's lower than that, guess again:"
else
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
exit
$PSQL "INSERT INTO games(user_id, number_of_guesses) VALUES('$USER_ID', '$NUMBER_OF_GUESSES');"
fi
done
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
Actually, when I move the insert statement before the echo command, i.e.
...
else
$PSQL "INSERT INTO games(user_id, number_of_guesses) VALUES('$USER_ID', '$NUMBER_OF_GUESSES');"
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
exit
fi
then not only test case #8 is failing, but also this one
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
Any idea what is going wrong here? I also wiped the database and recreated it and its tables, but I am receiving the same errors. This is why I would be curious to see the test results.
I suggest that you hard code the guess number to 9. Then run the code and make exactly 11 guesses. Guess starting from 0 to 9 in order but include one bad guess in there. (So total should be 11)
Go ahead and and capture that output with something like;
./nameofscript.sh 2>&1 | tee output.txt and then please copy the output.txt here for us thx.
Ps. Make sure you test the new version of the script. And share the full code again with corrections.
Not quite sure if I am following but I tried to fulfill your question.
number_guess.sh
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guessing_game -t --no-align -c"
echo "Enter your username:"
read USERNAME
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME';")
if [[ -z $USER_ID ]]; then
$PSQL "INSERT INTO users(username) VALUES('$USERNAME');"
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME';")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM games WHERE user_id=$USER_ID;")
BEST_GAME=$($PSQL "SELECT MIN(number_of_guesses) FROM games WHERE user_id=$USER_ID;")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
# SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
SECRET_NUMBER=9
NUMBER_OF_GUESSES=0
echo "Guess the secret number between 1 and 1000: "
while true; do
read GUESS
((NUMBER_OF_GUESSES++))
if ! [[ "$GUESS" =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
continue
fi
if [[ $GUESS -lt $SECRET_NUMBER ]]; then
echo "It's higher than that, guess again:"
elif [[ $GUESS -gt $SECRET_NUMBER ]]; then
echo "It's lower than that, guess again:"
else
$PSQL "INSERT INTO games(user_id, number_of_guesses) VALUES('$USER_ID', '$NUMBER_OF_GUESSES');"
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
exit
fi
done
and here is output.txt
Enter your username:
Welcome back, helloworld! You have played 1 games, and your best game took 11 guesses.
Guess the secret number between 1 and 1000:
It's higher than that, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
That is not an integer, guess again:
It's higher than that, guess again:
It's higher than that, guess again:
INSERT 0 1
You guessed it in 11 tries. The secret number was 9. Nice job!
I hope this helps. At least it does not seem like any error was produced.