I really liked the resource Relational Database, it is a very good primer on bash, git and sql. Three things that seemed daunting just a week ago but now seem more approachable. But the tests on the final project don’t seem well designed at all. I’ve decided to give up on trying to get the tests to pass because I feel like I’m wasting time that I could use to learn more.
My code is
number_guess.sh
#! /bin/bash
secret_number=$[RANDOM % 1000 + 1]
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align --csv -q -c"
echo "Enter your username:"
read username
IFS=',' read user_id games_played best_game <<<$($PSQL "SELECT user_id, COUNT(game_id), MIN(guesses) FROM users LEFT JOIN games USING(user_id) GROUP BY user_id HAVING username='$username'")
if [[ $user_id ]]
then
echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
else
user_id=$($PSQL "INSERT INTO users(username) VALUES('$username') RETURNING user_id")
echo "Welcome, $username! It looks like this is your first time here."
fi
game(){
echo $1
read guess
((number_of_guesses+=1))
if [[ $guess =~ ^[0-9]+$ ]]
then
if [[ $guess > $secret_number ]]
then
game "It's lower than that, guess again:"
fi
if [[ $guess < $secret_number ]]
then
game "It's higher than that, guess again:"
fi
else
game "That is not an integer, guess again:"
fi
}
game "Guess the secret number between 1 and 1000:"
$($PSQL "INSERT INTO games(user_id, guesses, number) VALUES($user_id, $number_of_guesses, $secret_number)")
echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
The postgres dump is here in my repo: freecodecamp/RDB/number_guess at main · lourendotco/freecodecamp · GitHub
Tests that don’t pass (all but the first one sometimes pass):
When you run your script, you should prompt the user for a username with Enter your username:, and take a username as input. Your database should allow usernames that are 22 characters
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
If the username has not been used before, you should print Welcome, <username>! It looks like this is your first time here.
The next line printed should be Guess the secret number between 1 and 1000: and input from the user should be read
If anything other than an integer is input as a guess, it should print That is not an integer, guess again:
Until they guess the secret number, it should print It's lower than that, guess again: if the previous input was higher than the secret number, and It's higher than that, guess again: if the previous input was lower than the secret number. Asking for input each time until they input the secret number.
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
Thanks in advance for any help