Hello, I’m struggling with passing 3 tests:
- 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 of at least 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 - 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!
I’ve read that we should specify the column username and I set it VARCHAR(25), but still cannot pass.
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
GUESS_LOOP(){
read GUESS_NUMBER
if [[ $GUESS_NUMBER =~ ^[0-9]+$ ]]
then
while [[ $THE_NUMBER == "NOT FOUND" ]]
do
ONE_TRY $RANDOM_NUMBER $GUESS_NUMBER
done
else
echo "That is not an integer, guess again:"
GUESS_LOOP
fi
}
ONE_TRY(){
if [ $1 -lt $2 ]
then
echo "It's lower than that, guess again:"
((TRIES++))
GUESS_LOOP
elif [ $1 -gt $2 ]
then
echo "It's higher than that, guess again:"
((TRIES++))
GUESS_LOOP
elif [ $1 -eq $2 ]
then
echo "You guessed it in $TRIES tries. The secret number was $RANDOM_NUMBER. Nice job!"
THE_NUMBER="FOUND"
ADD_GAMES=$($PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$USER_NAME'")
ADD_BEST_GAMES=$($PSQL "UPDATE users SET best_game = $TRIES WHERE username = '$USER_NAME'")
fi
}
GUESSING_GAME() {
TRIES=0
THE_NUMBER="NOT FOUND"
RANDOM_NUMBER=$(($RANDOM % 1000 + 1))
echo "Enter your username"
read USER_NAME
USER=$($PSQL "SELECT username, games_played, best_game FROM users WHERE username='$USER_NAME'")
if [[ -z $USER ]]
then
echo "Welcome, $USER_NAME! It looks like this is your first time here."
ADD_USER=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USER_NAME', '0', '0')")
echo "Guess the secret number between 1 and 1000:"
GUESS_LOOP
else
IFS="|" read USERNAME GAMES_PLAYED BEST_GAME <<< $USER
echo -e "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
echo "Guess the secret number between 1 and 1000:"
GUESS_LOOP
fi
}
GUESSING_GAME
And the DATABASE is simple:
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(25) NOT NULL,
games_played integer,
best_game integer
);GIT
As I understand the problem is output… Could you help me out with this?