Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Hello everyone! I am having issues with the welcome back message. I have already tried changing my code completely and redoing it with no luck. Please HELP!!

Your code so far

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c”

#ask for username
echo -e “\nEnter your username:”
read USERNAME

#check if username is empty
if [[ -z $USERNAME ]]
then
echo “Please enter a valid username.”
else
USER_ID=$($PSQL “SELECT user_id FROM users WHERE username = ‘$USERNAME’”)
#check if username exists
if [[ -z $USER_ID ]]
then
#if username does not exist, enter into db
INSERT_NEW_USER=$($PSQL “INSERT INTO users(username) VALUES(‘$USERNAME’)”)
echo -e “Welcome, $USERNAME! It looks like this is your first time here.”
else
#if username does exist
GAMES_PLAYED=$($PSQL “SELECT COUNT(*) FROM games WHERE user_id = $USER_ID”)
BEST_GAME=$($PSQL “SELECT MIN(guesses) FROM games where user_id = $USER_ID”)
echo -e “Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses”
fi
#create a random number between 0-1000
SECRET_NUMBER=$(( $RANDOM % 1001))
#ask user to guess number
echo -e “\nGuess the secret number between 1 and 1000:”

read NUMBER_GUESSED

NUMBER_OF_GUESSES=1

while [ $SECRET_NUMBER != $NUMBER_GUESSED ]
do
if [[ ! $NUMBER_GUESSED =~ [1]+$ ]]
then
echo -e “\nThat is not an integer, guess again:”
read NUMBER_GUESSED
else
#if guess is greater than random number
if [[ $SECRET_NUMBER < $NUMBER_GUESSED ]]
then
echo -e “\nIt’s lower than that, guess again:”
#if guess is less than random number
else
echo -e “\nIt’s higher than that, guess again:”
fi
NUMBER_OF_GUESSES=$(($NUMBER_OF_GUESSES+1))

  read NUMBER_GUESSED
fi

done
#store game information to database
INSERT_NEW_GAME=$($PSQL “INSERT INTO games(user_id, guesses) VALUES($USER_ID, $NUMBER_OF_GUESSES)”)
#if guess is equal to random number
echo “You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!”
exit
fi

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0

Challenge Information:

Number Guessing Game - Build a Number Guessing Game


  1. 0-9 ↩︎

Sorry! the most recent code is as follows. Everything passes except the welcome back message:

#!/bin/bash

PSQL=“psql --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c”

#ask for username
echo -e “\nEnter your username:”
read USERNAME

#check if username is empty
if [[ -z $USERNAME ]]
then
echo “Please enter a valid username.”
else
USER_ID=$($PSQL “SELECT user_id FROM users WHERE username = ‘$USERNAME’”)
#check if username exists
if [[ -z $USER_ID ]]
then
#if username does not exist, enter into db
INSERT_NEW_USER=$($PSQL “INSERT INTO users(username) VALUES(‘$USERNAME’)”)
USER_ID=$($PSQL “SELECT user_id FROM users WHERE username = ‘$USERNAME’”)
echo -e “Welcome, $USERNAME! It looks like this is your first time here.”
else
#if username does exist
GAMES_PLAYED=$($PSQL “SELECT COUNT(*) FROM games WHERE user_id = $USER_ID”)
BEST_GAME=$($PSQL “SELECT MIN(guesses) FROM games where user_id = $USER_ID”)
echo -e “Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses”
fi
#create a random number between 0-1000
SECRET_NUMBER=$(( $RANDOM % 1001))
#ask user to guess number
echo -e “\nGuess the secret number between 1 and 1000:”

read NUMBER_GUESSED

NUMBER_OF_GUESSES=1

while [ $SECRET_NUMBER != $NUMBER_GUESSED ]
do
if [[ ! $NUMBER_GUESSED =~ [1]+$ ]]
then
echo -e “\nThat is not an integer, guess again:”
read NUMBER_GUESSED
else
#if guess is greater than random number
if [[ $SECRET_NUMBER < $NUMBER_GUESSED ]]
then
echo -e “\nIt’s lower than that, guess again:”
#if guess is less than random number
else
echo -e “\nIt’s higher than that, guess again:”
fi
NUMBER_OF_GUESSES=$(($NUMBER_OF_GUESSES+1))

  read NUMBER_GUESSED
fi

done
#store game information to database
INSERT_NEW_GAME=$($PSQL “INSERT INTO games(user_id, guesses) VALUES($USER_ID, $NUMBER_OF_GUESSES)”)
#if guess is equal to random number
echo “You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!”
exit
fi


  1. 0-9 ↩︎