Build a Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I have been trying to fulfil one of the checkpoint but it is not fulfilled no matter what
“If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses., with 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”

Your code so far

Your browser information:

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

Challenge Information:

Build a Number Guessing Game - Build a Number Guessing Game

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-number-guessing-game/602da04c22201c65d2a019f4.md at main · freeCodeCamp/freeCodeCamp · GitHub

“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”

this checklist is also not getting fulfilled

please share your code, otherwise it is impossible to help you

number_guess.sh →

#!/bin/bash

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

SECRET_NUMBER=$((RANDOM % 1000 + 1))

GUESS_COUNT=0

echo "Enter your username:"

read USERNAME

USER_COUNT=$($PSQL "SELECT COUNT(*) FROM users WHERE username = '$USERNAME';")

if [[ $USER_COUNT -eq 0 ]]; then

echo "Welcome, $USERNAME! It looks like this is your first time here."

$PSQL "INSERT INTO users (username, games_played) VALUES ('$USERNAME', 0);"

else

GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username = '$USERNAME';" | xargs)

BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME';" | xargs)

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

fi

echo "Guess the secret number between 1 and 1000:"

while true; do

read GUESS

if ! [[ 
 ]]; then

echo "That is not an integer, guess again:"

continue

fi

GUESS_COUNT=$((GUESS_COUNT + 1))

if [[ $GUESS -eq $SECRET_NUMBER ]]; then

echo "You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"

$PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$USERNAME';"

USER_BEST=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME';")

if \[\[ -z $USER_BEST || $USER_BEST == "" \]\]; then

  $PSQL "UPDATE users SET best_game = $GUESS_COUNT WHERE username = '$USERNAME';"

elif \[\[ $GUESS_COUNT -lt $USER_BEST \]\]; then

  $PSQL "UPDATE users SET best_game = $GUESS_COUNT WHERE username = '$USERNAME';"

fi

break

elif [[ $GUESS -gt $SECRET_NUMBER ]]; then

echo "It's lower than that, guess again:"

else

echo "It's higher than that, guess again:"

fi

done

When I copy pasted some indentations got removed
So please ignore that

Hi @svetlana.neogi,

Please re-post your code formatted as follows:

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

Happy coding

#!/bin/bash

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

# Generate random number between 1 and 1000
SECRET_NUMBER=$((RANDOM % 1000 + 1))
GUESS_COUNT=0

# Get username
echo "Enter your username:"
read USERNAME

# Check if user exists
USER_COUNT=$($PSQL "SELECT COUNT(*) FROM users WHERE username = '$USERNAME';")

if [[ $USER_COUNT -eq 0 ]]; then
  # New user
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "INSERT INTO users (username, games_played) VALUES ('$USERNAME', 0);"
else
  # Returning user - query each value separately
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username = '$USERNAME';" | xargs)
  BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME';" | xargs)
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

echo "Guess the secret number between 1 and 1000:"
while true; do
  read GUESS
  
  # Check if input is an integer
  if ! [[ $GUESS =~ ^[0-9]+$ ]]; then
    echo "That is not an integer, guess again:"
    continue
  fi
  
  GUESS_COUNT=$((GUESS_COUNT + 1))
  
  if [[ $GUESS -eq $SECRET_NUMBER ]]; then
    echo "You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"
    # Update database
    $PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$USERNAME';"
    USER_BEST=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME';")
    if [[ -z $USER_BEST || $USER_BEST == "" ]]; then
      $PSQL "UPDATE users SET best_game = $GUESS_COUNT WHERE username = '$USERNAME';"
    elif [[ $GUESS_COUNT -lt $USER_BEST ]]; then
      $PSQL "UPDATE users SET best_game = $GUESS_COUNT WHERE username = '$USERNAME';"
    fi
    break
  elif [[ $GUESS -gt $SECRET_NUMBER ]]; then
    echo "It's lower than that, guess again:"
  else
    echo "It's higher than that, guess again:"
  fi
done

@dhess @ILM It’s been 6 days, I even gave the code you asked for. Please help me out

Hi @svetlana.neogi

Can you please play one game and show the output?

Happy coding