Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I am running into issues with user stories #8 and #13 on the random number guessing game. All other stories pass. There are many forum posts about these stories but none of those suggestions have worked for me.

#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

#13: 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

Your code so far

#!/bin/bash

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

echo "Enter your username:"
read USERNAME

# Check if user exists
USER_EXISTS=$($PSQL "SELECT username FROM user_data WHERE username='$USERNAME'")

if [[ -z $USER_EXISTS ]]
then
  # New user
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "INSERT INTO user_data(username, games_played, best_game) VALUES('$USERNAME', 0, 0)"
else
  # Get user stats
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM user_data WHERE username='$USERNAME'")
  BEST_GAME=$($PSQL "SELECT best_game FROM user_data WHERE username='$USERNAME'")
  
  # Display welcome back message with exact format
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# Generate secret number
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
NUMBER_OF_GUESSES=0

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

# Loop until correct guess
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
  
  # Increment guess counter
  ((NUMBER_OF_GUESSES++))
  
  # Check guess against secret number
  if [[ $GUESS -eq $SECRET_NUMBER ]]
  then
    # User guessed correctly - break out of loop
    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

# Display success message with exact format required by the test
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"

# Update user stats
$PSQL "UPDATE user_data SET games_played = games_played + 1 WHERE username='$USERNAME'"

# Update best game if necessary
if [[ $BEST_GAME -eq 0 || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
then
  $PSQL "UPDATE user_data SET best_game = $NUMBER_OF_GUESSES WHERE username='$USERNAME'"
fi

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Welcome back to the forum @mger1608

Check how you coded the Postgres commands.
Do they all follow the same format?

Happy coding

Interesting, not sure why that would have made a difference but it seems to have worked. Thank you!

1 Like

Did you test with and without the code change? What did you notice?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.