Build a Number Guessing Game SQL Test 8

I have gotten all tests to pass besides

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 cannot figure out why. It does the thing as far as I can tell.

Here is my bash script

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=usernames -t --no-align -c”

NUMBER=$(( RANDOM % 1000 + 1 ))

echo “Enter your username:”

read USERNAME

USERNAME3=$($PSQL “SELECT username FROM usernames WHERE username = ‘$USERNAME’”)

GAMES=$($PSQL “SELECT games_played FROM usernames WHERE username = ‘$USERNAME’”)

BEST=$($PSQL “SELECT tries FROM usernames WHERE username = ‘$USERNAME’”)

if [[ -z $USERNAME3 ]]

then

$PSQL “INSERT INTO usernames(username, games_played) VALUES(‘$USERNAME’, 0)” >/dev/null 2>&1

echo “Welcome, $USERNAME! It looks like this is your first time here.”

fi

if [[ $GAMES -gt 0 ]]

then

echo “Welcome back, $USERNAME3! You have played $GAMES games, and your best game took $BEST guesses.”

fi

echo “Guess the secret number between 1 and 1000:”

GUESSES=0

GUESSER() {

read GUESS

if ! [[ $GUESS =~ ^[0-9]+$ ]]

then

echo “That is not an integer, guess again:”

GUESSER

fi

if [[ $GUESS -lt $NUMBER ]]

then

echo “It’s higher than that, guess again:”

((GUESSES++))

GUESSER

fi

if [[ $GUESS -gt $NUMBER ]]

then

echo “It’s lower than that, guess again:”

((GUESSES++))

GUESSER

fi

if [[ $GUESS -eq $NUMBER ]]

then

((GUESSES++))

$PSQL “UPDATE usernames SET games_played = games_played + 1 WHERE username = ‘$USERNAME’” >/dev/null 2>&1

BEST2=$($PSQL “SELECT tries FROM usernames WHERE username = ‘$USERNAME’”)

if [[ -z $BEST2 || $GUESSES -lt $BEST2 ]]

then

$PSQL “UPDATE usernames SET tries = $GUESSES WHERE username = ‘$USERNAME’”

fi

echo “You guessed it in $GUESSES tries. The secret number was $NUMBER. Nice job!”

exit

fi

}

GUESSER

Seems like it wanted a while loop and not a recursive call.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.