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
#!/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.
Hello! I tried changing it as you suggested but it didnt work
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
#!/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:
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.”
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)