Tell us what’s happening:
I finished the project, as far as I know the game is fully functional, but it’s not passing some of the tests.
I tried running them multiple times, sometimes it passes some tests and sometimes it doesn’t.
I’m not sure if there is something wrong with my code or if I need to keep clicking the Run button until it passes.
Your code so far
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# generate random number between 1 and 1000
RANDOM_NUMBER=$(( $RANDOM % 1000 + 1 ))
# ask for username
echo -e "\nEnter your username:\n"
read USERNAME
GET_USER_DATA=$($PSQL "SELECT * FROM users WHERE username = '$USERNAME'")
# if username is new
if [[ -z $GET_USER_DATA ]]
then
# save into DB
SAVE_NEW_USER=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 1, 1000)")
echo -e "\nWelcome, $USERNAME! It looks like this is your first time here.\n"
# if username has been used before
else
echo "$GET_USER_DATA" | while IFS=" |" read USERNAME GAMES_PLAYED BEST_GAME
do
echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.\n"
done
fi
GUESS_COUNT=0
# ask user to guess number
GUESS_NUMBER() {
echo -e "\nGuess the secret number between 1 and 1000:\n"
let GUESS_COUNT++
read USER_GUESS
# if user input is not an integer
if ! [[ $USER_GUESS =~ ^[0-9]+$ ]]
then
echo -e "\nThat is not an integer, guess again:\n"
GUESS_NUMBER
fi
# if user input is lower
if [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
then
echo -e "\nIt's higher than that, guess again:\n"
GUESS_NUMBER
fi
# if user input is higher
if [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
then
echo -e "\nIt's lower than that, guess again:\n"
GUESS_NUMBER
fi
}
GUESS_NUMBER
# if user input is correct
if [[ $USER_GUESS -eq $RANDOM_NUMBER ]]
then
GET_USER_DATA=$($PSQL "SELECT * FROM users WHERE username = '$USERNAME'")
echo "$GET_USER_DATA" | while IFS=" |" read USERNAME GAMES_PLAYED BEST_GAME
do
# if number of guesses is lower than the best game
if [[ $GUESS_COUNT -lt $BEST_GAME ]]
then
# save new best game and game count
NEW_BEST_GAME=$($PSQL "UPDATE users SET best_game=$GUESS_COUNT, games_played=$GAMES_PLAYED + 1 WHERE username = '$USERNAME'")
fi
done
echo -e "\nYou guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!\n"
fi
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Challenge: Number Guessing Game - Build a Number Guessing Game
I edited your code to make it legible on the forum. When you post code, it should be enclosed within two sets of triple backticks. You can do this manually or use the Preformatted Text tool (</> icon or CTRL+e) to create them for you.
Do you have an SQL dump which you can share too? I can test your code and see what might be going wrong.
I’ll keep trying and seaching in the forum for similar issues, but if anybody could take a look it would be greatly appreciated!
Here’s my code so far:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# generate random number between 1 and 1000
RANDOM_NUMBER=$(( $RANDOM % 1000 + 1 ))
# ask for username
echo "Enter your username:"
read USERNAME
GET_USER_DATA=$($PSQL "SELECT * FROM users WHERE username = '$USERNAME'")
# if username is new
if [[ -z $GET_USER_DATA ]]
then
# save into DB
SAVE_NEW_USER=$($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USERNAME', 0, 1000)")
echo "Welcome, $USERNAME! It looks like this is your first time here."
# if username has been used before
else
echo "$GET_USER_DATA" | while IFS=" |" read USERNAME GAMES_PLAYED BEST_GAME
do
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
done
fi
GUESS_COUNT=0
# ask user to guess number
GUESS_NUMBER() {
echo "Guess the secret number between 1 and 1000:"
let GUESS_COUNT++
read USER_GUESS
# if user input is not an integer
if ! [[ $USER_GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
GUESS_NUMBER
fi
# if user input is lower
if [[ $USER_GUESS -lt $RANDOM_NUMBER ]]
then
echo "It's higher than that, guess again:"
GUESS_NUMBER
fi
# if user input is higher
if [[ $USER_GUESS -gt $RANDOM_NUMBER ]]
then
echo "It's lower than that, guess again:"
GUESS_NUMBER
fi
}
GUESS_NUMBER
# if user input is correct
if [[ $USER_GUESS -eq $RANDOM_NUMBER ]]
then
GET_USER_DATA=$($PSQL "SELECT best_game FROM users WHERE username = '$USERNAME'")
echo "$GET_USER_DATA" | while IFS=" |" read BEST_GAME
do
# if number of guesses is lower than the best game
if [[ $GUESS_COUNT -lt $BEST_GAME ]]
then
# save new best game and game count
NEW_BEST_GAME=$($PSQL "UPDATE users SET best_game=$GUESS_COUNT, games_played=games_played + 1 WHERE username = '$USERNAME'")
else
SAVE_GAME=$($PSQL "UPDATE users SET games_played=games_played + 1 WHERE username='$USERNAME'")
fi
done
echo "You guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!"
fi
Sorry, I have been looking at your code but I can’t get it to pass either at the moment. I did notice a couple of errors, but you appear to have fixed those now (e.g. games_played only updating when a new best score is achieved).
I have also been playing around with the timeouts and whatnot but to no avail, yet. I’ll let you know if I figure it out.
Ok, I got your code to pass by making one change… I think it’s fair to give you the correction here as it’s a fault of the testing environment rather than bad code on your part.
Change this:
GUESS_COUNT=0
# ask user to guess number
GUESS_NUMBER() {
echo "Guess the secret number between 1 and 1000:"
let GUESS_COUNT++
read USER_GUESS
To this:
GUESS_COUNT=0
echo "Guess the secret number between 1 and 1000:"
GUESS_NUMBER() {
read USER_GUESS
let GUESS_COUNT++
It means that you’re not prompting the user with this additional message each time they guess incorrectly, and also that the prompt to guess again is immediately followed by reading user input.
If your code still doesn’t pass, then it’s just a question of tweaking timeout values more. Let me know if you need any help with that.