Hello! I have a problem concerning the Guessing game we have to make for the last project in Relational Database. My script works just fine and I have no problem what so ever to make it run. However it seems like freecodecamp won’t validate the following points:
-
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 -
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
I have tried other scripts from others and it still won’t validate my project. Here is my code, please if you can help me it would be great:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo "Enter your username: "
read USERNAME
RANDOM_NUMBER=$((1 + RANDOM % 1000))
SELECTED_USER=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME'")
#if username not found
if [[ -z $SELECTED_USER ]]
then
INSERT_USERNAME=$($PSQL "INSERT INTO users (username) VALUES('$USERNAME')")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
#if username found
GAMES_PLAYED=$($PSQL "SELECT COUNT(*)
FROM games
JOIN users ON games.user_id = users.user_id
WHERE users.username = '$USERNAME';")
BEST_GUESS=$($PSQL "SELECT MIN(guesses) FROM games
JOIN users ON games.user_id = users.user_id
WHERE users.username = '$USERNAME';")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GUESS guesses. "
fi
#GRAB USER ID
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")
TRIES=0
GUESS=0
#echo $USER_ID
#echo "$RANDOM_NUMBER"
until [ $RANDOM_NUMBER == $GUESS ]
do
echo "Guess the secret number between 1 and 1000:"
read GUESS
if [[ ! $GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
else
(( TRIES++ ))
if [[ $GUESS -gt $RANDOM_NUMBER ]]
then
echo "It's lower than that, guess again:"
elif [[ $GUESS -lt $RANDOM_NUMBER ]]
then
echo "It's higher than that, guess again:"
fi
fi
done
#insert data
INSERTED_GAME_INFO=$($PSQL "INSERT INTO games (user_id, guesses) VALUES ($USER_ID, $TRIES)")
#if equal to random_number
echo "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job! "