Tell us what’s happening:
All tests are passing except for the printing tests.
Your code so far
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# Generate a random number between 1 and 1000
secret_number=$(( $RANDOM % 1000 + 1 ))
# Prompt the user for a username
echo "Enter your username:"
read username
# Check if the username has been used before
if [[ -n $($PSQL "SELECT username FROM users WHERE username = '$username'") ]]; then
# Get the user's information from the database
id=$($PSQL "SELECT id FROM users WHERE username = '$username'")
games_played=$($PSQL "SELECT games_played FROM games WHERE id = '$id'")
best_game=$($PSQL "SELECT best_game FROM games WHERE id = '$id'")
echo -e "\nWelcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
else
echo -e "\nWelcome, $username! It looks like this is your first time here."
# Insert the username into the database
$PSQL "INSERT INTO users (username) VALUES ('$username')"
id=$($PSQL "SELECT id FROM users WHERE username = '$username'")
$PSQL "INSERT INTO games (id) VALUES ('$id')"
best_game=$($PSQL "SELECT best_game FROM games WHERE id = '$id'")
fi
# Prompt the user to guess the secret number
echo "Guess the secret number between 1 and 1000:"
read guess
# Initialize the number of guesses
number_of_guesses=1
# Loop until the user guesses the secret number
while [[ $guess -ne $secret_number ]]; do
# Check if the guess is an integer
if [[ ! $guess =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
read guess
fi
# Check if the guess is higher or lower than the secret number
if [[ $guess -gt $secret_number ]]; then
echo "It's lower than that, guess again:"
else
echo "It's higher than that, guess again:"
fi
# Increment the number of guesses
number_of_guesses=$((number_of_guesses + 1))
# Prompt the user to guess again
read guess
done
# Update the games played in the database
$PSQL "UPDATE games SET games_played = games_played + 1 WHERE id = '$id'"
# Update the best game if necessary
if [[ $number_of_guesses -lt $best_game ]]; then
$PSQL "UPDATE games SET best_game = $number_of_guesses WHERE id = '$id'"
fi
echo -e "\nYou guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Number Guessing Game - Build a Number Guessing Game
What I Have Tried:
I have tried ensuring I am using 2 tables in the db, 1 for games and 1 for users. I am ensuring all the whitespace is correct. This is now the second task where I pass all tests except for the printing aspect. Am I doing something wrong here?