Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I’ve been trying to complete the Number Guessing Game on freeCodeCamp. I’m absolutely sure I completed ALL of the tests. The script works just fine and is completely functional. But I haven’t gotten further than “your script should prompt for a username.” The database is completely working. Am I supposed to format it a certain way, and if so, can you clarify for me?

From, @lickington

Your code so far

#!/bin/bash
MAX=1000
MIN=1
GUESSES=0
SECRET_NUMBER=$((RANDOM % (MAX - MIN + 1) + MIN))
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
read -p "Enter your username:" USERNAME
USERNAME_EXISTS=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")

if [[ -z $USERNAME_EXISTS ]]; then
  INSERT_USERNAME=$($PSQL "INSERT INTO users (username, best_game, games_played) VALUES ('$USERNAME', 0, 0);")

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

else
  BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME'")
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username = '$USERNAME'")  
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
read -p "Guess the secret number between 1 and 1000:" GUESS

until [[ $GUESS -eq $SECRET_NUMBER ]]
do
 if [[ $GUESS =~ ^-?[0-9]+$ ]]; then
  if [[ $GUESS -lt $SECRET_NUMBER ]]; then
   read -p "It's higher than that, guess again:" GUESS
   ((GUESSES++))
  else
   read -p "It's lower than that, guess again:" GUESS
   ((GUESSES++))
  fi
  else
   read -p "That is not an integer, guess again:" GUESS
  fi
done
((GUESSES++))
((GAMES_PLAYED++))
  if [[ $BEST_GAME -eq 0 || $GUESSES -lt $BEST_GAME ]]; then
   
   UPDATE_BEST_GAME=$($PSQL "UPDATE users SET best_game = $GUESSES, games_played = $GAMES_PLAYED")

  else
   UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET games_played = $GAMES_PLAYED")
  fi
 echo "You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Hi @cybershiloh

Try removing the -p switch from the read command.

Happy coding

1 Like

Hello again, @Teller!
Okay, so I just tried this again (removing the -p switches), and the test checker gave me an error: “Test Runner Failed”. What does that mean?

Try separating the two commands.
An echo for the message, then a read for the variable.

Not sure about the error message you received.

Happy coding

It IS working for the most part, but there’s still one test unchecked: If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses., with 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.

Here’s my code so far:

#!/bin/bash
MAX=1000
MIN=1
GUESSES=0
SECRET_NUMBER=$((RANDOM % (MAX - MIN + 1) + MIN))
# asks for a username
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo "Enter your username:"
read USERNAME
USERNAME_EXISTS=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")

if [[ -z $USERNAME_EXISTS ]]; then
  INSERT_USERNAME=$($PSQL "INSERT INTO users (username, best_game, games_played) VALUES ('$USERNAME', 0, 0);")

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

else
  BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME'")
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username = '$USERNAME'")  
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
# generates a secret number
echo "Guess the secret number between 1 and 1000:"
read GUESS

until [[ $GUESS -eq $SECRET_NUMBER ]]
do
 if [[ $GUESS =~ ^-?[0-9]+$ ]]; then
  if [[ $GUESS -lt $SECRET_NUMBER ]]; then
   echo "It's higher than that, guess again:"
   read GUESS
   ((GUESSES++))
  else
   echo "It's lower than that, guess again:"
   read GUESS
   ((GUESSES++))
  fi
  else
   echo "That is not an integer, guess again:"
   read GUESS
  fi
done
((GUESSES++))
((GAMES_PLAYED++))
# code to update the best game
  if [[ $BEST_GAME -eq 0 || $GUESSES -lt $BEST_GAME ]]; then
   
   UPDATE_BEST_GAME=$($PSQL "UPDATE users SET best_game = $GUESSES, games_played = $GAMES_PLAYED")

  else
   UPDATE_GAMES_PLAYED=$($PSQL "UPDATE users SET games_played = $GAMES_PLAYED")
  fi
 echo "You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"

I’m pretty sure that I checked for spelling errors. What did I do wrong?

To help debug, try playing one game.
Check the users table contains the correct information after the game.

1 Like

A post was split to a new topic: Number Guessing Game - Build a Number Guessing Game

I should’ve known – the database “didn’t exist”!!! All I had to do was just restart the database! Thanks for your help, @Teller!

1 Like