Number Guessing game can't pass the test

I can’t pass the test :

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

This is my script:

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -c”

echo “Enter your username:”

read USERNAME

USER_ID=$($PSQL “SELECT user_id FROM users WHERE username=‘$USERNAME’;”)

if [[ -n $USER_ID ]]

then

GAMES_PLAYED=$($PSQL “SELECT COUNT(*) FROM games WHERE user_id=$USER_ID;”)

BEST_GAME=$($PSQL “SELECT MIN(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.”

else

$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.”

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!"

$PSQL "INSERT INTO games(user_id, guesses) VALUES($USER_ID, $NUMBER_OF_GUESSES);"

break

fi

done

I have also added my script as a photo:

Welcome back to the forum @yildizbas.nur

Can you post a screen grab showing the welcome back message for a user?

Happy coding

This is how it looks:

I just realised my games table looks like this which is weird, since there are 0’s under guesses column, also I have two tables in total one is users second is games. The users table has username column and user_id column, I have also been getting errors for this 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

So there is a bit to unpack here.

Why are there 23 game_id’s.

Did one of the games take 999 guesses?

they automatically appeared in my table after I created the games and users table . I have no idea.

I have fixed it . It was a problem with my output, I had to eliminate INSERT 0 1

1 Like