Tell us what’s happening:
I did all of the code needed and tested it and there is always the same two tests that are not working since I’ve checked thousands of times and I haven’t found anything wrong with my code!
Tests that are failing:
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.
&
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 browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
My code so far:
For the capture of the name of the user:
# PSQL variable for database queries
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# Prompt for username
echo "Enter your username:"
read USERNAME
# Ensure username is less than or equal to 22 characters
if [[ ${#USERNAME} -gt 22 ]]; then
echo "Error: Username cannot be longer than 22 characters."
exit 1
fi
# Check if username exists in the database
USER_INFO=$($PSQL "SELECT games_played, best_game FROM users WHERE username='$USERNAME'")
# If user exists
if [[ -n "$USER_INFO" ]]; then
# Parse user information
IFS='|' read GAMES_PLAYED BEST_GAME <<< "$USER_INFO"
# Greet returning user
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
# If user does not exist, insert them into the database
INSERT_USER_RESULT=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
echo "Welcome, $USERNAME! It looks like this is your first time here."
fi
For the evaluation of the guessed number and the printing of the successful guess:
# Generate a random number between 1 and 1000
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
# Output to indicate the game has started
echo "Guess the secret number between 1 and 1000:"
# Initialize guess counter
GUESS_COUNT=0
GUESS=0
# Function to check if input is an integer
is_integer() {
[[ "$1" =~ ^[0-9]+$ ]]
}
# Loop to take guesses
while [[ $GUESS -ne $SECRET_NUMBER ]]; do
GUESS_COUNT=$(( GUESS_COUNT + 1 ))
echo "Enter your guess:"
# Read user input
read GUESS
# Check if input is an integer
if ! is_integer "$GUESS"; then
echo "That is not an integer, guess again:"
continue
fi
# Compare the guess with the secret number
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:"
fi
done
# Once the user guesses correctly, update the database
echo "You guessed it in $GUESS_COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"