Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Output is correct but tests are did not complete.

The output for : 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

are correct but tests are not being completed

Your code so far

if [[ -n “$QUERY_USERNAME_RESULT” ]]; then

IFS='|' read -r DB_USERNAME GAMES_PLAYED BEST_GAME <<< "$QUERY_USERNAME_RESULT"

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

else
echo “Welcome, $USERNAME! It looks like this is your first time here.”
fi

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

I checked for spaces etc. I literally copy-pasted the texts and replaced the variables

#!/bin/bash

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

#  Prompt the user for their  username
echo "Enter your username:"
read USERNAME

# SQL query to get the username,  games_played and best_game

SQL_QUERY="SELECT username, games_played, best_game FROM users WHERE username ILIKE '$USERNAME';"


QUERY_USERNAME_RESULT=$($PSQL "$SQL_QUERY")


QUERY_USERNAME_RESULT=$(echo "$QUERY_USERNAME_RESULT" | xargs)




if [[ -n "$QUERY_USERNAME_RESULT" ]]; then
    # Extract the columns from the query result
    IFS='|' read -r DB_USERNAME GAMES_PLAYED BEST_GAME <<< "$QUERY_USERNAME_RESULT"
    
       
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
    echo "Welcome, $USERNAME! It looks like this is your first time here."
fi

#Guessing number :

GENERATED_NUM=$(( (RANDOM % 1000) + 1 ))  # Secret number between 1 and 1000
NUMBER_OF_GUESSES=0

echo "Welcome to the guessing game! Try to guess the number between 1 and 1000."

while true; do
   
    echo "Guess the secret number between 1 and 1000:"
    read PLAYER_GUESS

    # Check if the input is a valid integer

    if [[ $PLAYER_GUESS =~ ^-?[0-9]+$ ]] && (( PLAYER_GUESS > 0 && PLAYER_GUESS <= 1000 )); then
       
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

        
        if (( PLAYER_GUESS < GENERATED_NUM )); then
            echo "It's higher than that, guess again:"
        elif (( PLAYER_GUESS > GENERATED_NUM )); then
            echo "It's lower than that, guess again:"
        else
            echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GENERATED_NUM. Nice job!"
            break
        fi
    else
        echo "That is not an integer, guess again:"
    fi
done

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

you need to count these bad guesses too.

1 Like

Hello! I tried changing it as you suggested but it didnt work :frowning:

while true; do
   
    echo "Guess the secret number between 1 and 1000:"
    read PLAYER_GUESS

    
    if [[ $PLAYER_GUESS =~ ^-?[0-9]+$ ]] && (( PLAYER_GUESS > 0 && PLAYER_GUESS <= 1000 )); then
        
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

        
        if (( PLAYER_GUESS < GENERATED_NUM )); then
            echo "It's higher than that, guess again:"
        elif (( PLAYER_GUESS > GENERATED_NUM )); then
            echo "It's lower than that, guess again:"
        else
            echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GENERATED_NUM. Nice job!"
            break
        fi
    else
        echo "That is not an integer, guess again:"
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))
    fi
done

how about the best game situation? How are you figuring out which game was the best?

This is the entire script


#!/bin/bash

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


echo "Enter your username:"
read USERNAME


SQL_QUERY="SELECT username, games_played, best_game FROM users WHERE username ILIKE '$USERNAME';"


QUERY_USERNAME_RESULT=$($PSQL "$SQL_QUERY")


QUERY_USERNAME_RESULT=$(echo "$QUERY_USERNAME_RESULT" | xargs)



if [[ -n "$QUERY_USERNAME_RESULT" ]]; then
    
    IFS='|' read -r DB_USERNAME GAMES_PLAYED BEST_GAME <<< "$QUERY_USERNAME_RESULT"
    
      
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
    echo "Welcome, $USERNAME! It looks like this is your first time here."
fi

#Guessing number :

GENERATED_NUM=$(( (RANDOM % 1000) + 1 ))  
NUMBER_OF_GUESSES=0

echo "Welcome to the guessing game! Try to guess the number between 1 and 1000."

while true; do
   
    echo "Guess the secret number between 1 and 1000:"
    read PLAYER_GUESS

    
    if [[ $PLAYER_GUESS =~ ^-?[0-9]+$ ]] && (( PLAYER_GUESS > 0 && PLAYER_GUESS <= 1000 )); then
        
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

        
        if (( PLAYER_GUESS < GENERATED_NUM )); then
            echo "It's higher than that, guess again:"
        elif (( PLAYER_GUESS > GENERATED_NUM )); then
            echo "It's lower than that, guess again:"
        else
            echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $GENERATED_NUM. Nice job!"
            break
        fi
    else
        echo "That is not an integer, guess again:"
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))
    fi
done

To figure out the best game you have to find the game row that has the least number of guesses. You should not be storing that in the database as a field.

Not sure what you meant by “storing”. I am not inserting anything to the database, all rows were generated by the test. To me it seems that the output is exactly what it should be, i don’t understand what is going wrong tbh.

If someone plays the game twice with the same username, how do you figure out which game was the best one if he plays a third time? I can’t see any code that does that above.

I also wanted to share the link to the test code in case it helps to review it:

Can you also remove the extra text here and make it match the required text only?

1 Like

Hmm i did think at some point that it made no sense to NOT store the best game after each game ends but i didn’t find anything about that in the stories so i skipped. I’ll try and do that.

also i tried removing the extra text but nothing changed

echo “Welcome to the guessing game! Try to guess the number between 1 and 1000.”

you wanna store all the games and find the minimum plays through a query. That’s how I believe it is expected to happen.

I added this update at the end of the game. It works properly but now everything else broke.

Table before the game was played:

type or paste code here

Updated table after the game was played, added a counter for games_played too:

I just added this in the end:

GAMES_PLAYED=$((GAMES_PLAYED + 1))

if [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]; then
    BEST_GAME=$NUMBER_OF_GUESSES
fi

UPDATE_QUERY="UPDATE users SET games_played = $GAMES_PLAYED, best_game = $BEST_GAME WHERE username = '$USERNAME';"
$PSQL "$UPDATE_QUERY"

Now instead of 2 stories to complete, i need to complete 7 of them.
Uploading: image.png…

From all that I have seen of people posting here, the correct way to do this is to store all the games and use a query to get the game with the minimum number of guesses at the start of each new game instead of storing the best game as a field in the table.
Take a look at the test code I linked to and see if you are able to run the test in steps on your end to see the output of each part and determine why it is failing. (The same file should be in .freecodecamp directory somewhere on gitpod)

Edit: also make sure the last line of code before you edit is the one that prints the message to the player about the correct guess number. (The table update should happen before the printing)

Yea thanks i just had to create another table for the games and store all the number_of_guesses there… and then query the best games from there

1 Like