9/13:
I am still having issues with the Number Guessing Game. I can load other lessons fine, however, the one I need to complete does not load. Does not load on FCC, and does not load on CodeAlly.io. I even tried opening the page from an incognito window, still no good.
9/15:
The project loaded, and I was able to progress for about 15mins before the PSQL machine went into read-only mode.
9/16:
Again today I am unable to load the project at all.
**9/19:
This is the last project in the course before I can claim the certification. I could have been done with this last week lol. I hope this can get sorted out soon.
on my end I can start codeally but I had to do some reloading.
(when I click on start the project, I go to a blank page, I wait a bit, nothing happens, then i reload and I end up back at fCC so I click on start the project again, this time I am taken to a page with the green codeally button, i click that and I am able to get in)
Tried again today. I was able to get into the project by clearing the CodeAlly cookie. Still had trouble with the VM working properly. I Deleted the entire container on CodeAlly’s website for a clean slate. Redid my work and still getting errors:
I can post the script, but this error has nothing to do with the script. Its a PSQL server connection error.
Also, since this is a project there aren’t any hints.
#!/bin/bash
# Number Guessing Game
PSQL="psql -X --username=freecodecamp --dbname=number_guessing_game --tuples-only -c"
# functions
# main guess
MAIN_GUESS() {
read GUESS
if [[ $GUESS =~ ^[0-9]*$ ]]
then
echo "Main guess: $GUESS"
GAME_LOOP $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES $PSQL
else
echo "That is not an integer, guess again:"
MAIN_GUESS
fi
}
# loop function
GAME_LOOP() {
GUESS=$1
SECRET_NUMBER=$2
NUMBER_OF_TRIES=$3
PSQL=$4
((NUMBER_OF_TRIES++))
echo -e "\nLoop guess: $GUESS\nLoop answer: $SECRET_NUMBER\nLoop tries: $NUMBER_OF_TRIES"
# correct pass
if [[ $GUESS == $SECRET_NUMBER ]]
then
GAMES_PLAYED=$($PSQL "SELECT game_id FROM guessing_game WHERE username = $USERNAME ORDER BY game_id DESC LIMIT 1")
BEST_GAME=$($PSQL "SELECT score FROM guessing_game WHERE username = '$USERNAME' ORDER BY score DESC LIMIT 1")
ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('$USERNAME', $NUMBER_OF_TRIES)")
echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
else
if [[ $GUESS > $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
else
if [[ $GUESS < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
fi
fi
fi
}
# start of game test-mode
NUMBER_OF_TRIES=0
echo "test: Number of tries: $NUMBER_OF_TRIES"
SECRET_NUMBER=$(( RANDOM % 1000 +1))
echo "test: Answer is $SECRET_NUMBER"
echo -e "\n~~~~~ Number Guessing Game ~~~~~\n"
# get username
echo "Enter your username:"
read USERNAME
USERNAME_RESULT=$($PSQL "SELECT username FROM guessing_game WHERE username = '$USERNAME'")
# if no username in database
if [[ -z $USERNAME_RESULT ]]
then
# welcome message
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
echo "Welcome back, $USERNAME!"
fi
echo "Guess the secret number between 1 and 1000:"
MAIN_GUESS
#!/bin/bash
# Number Guessing Game
PSQL="psql -X --username=freecodecamp --dbname=number_guess --tuples-only -c"
# functions
# main guess
MAIN_GUESS() {
read GUESS
if [[ $GUESS =~ ^[1-9][0-9]*$ ]]
then
echo "Main guess: $GUESS"
GAME_LOOP $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES $PSQL
else
echo "That is not an integer, guess again:"
MAIN_GUESS
fi
}
# loop function
GAME_LOOP()
{
GUESS=$1
SECRET_NUMBER=$2
NUMBER_OF_TRIES=$3
PSQL=$4
((NUMBER_OF_TRIES++))
echo -e "\nLoop guess: $GUESS\nLoop answer: $SECRET_NUMBER\nLoop tries: $NUMBER_OF_TRIES"
# correct pass
if [[ $GUESS == $SECRET_NUMBER ]]
then
ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('$USERNAME', $NUMBER_OF_TRIES)")
echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
else
if [[ $GUESS > $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
else
if [[ $GUESS < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
fi
fi
fi
}
# start of game test-mode
NUMBER_OF_TRIES=0
echo "test: Number of tries: $NUMBER_OF_TRIES"
SECRET_NUMBER=$(( RANDOM % 1000 +1))
echo "test: Answer is $SECRET_NUMBER"
echo -e "\n~~~~~ Number Guessing Game ~~~~~\n"
# get username
echo "Enter your username:"
read USERNAME
USERNAME_RESULT=$($PSQL "SELECT username FROM guessing_game WHERE username = '$USERNAME'")
# if no username in database
if [[ -z $USERNAME_RESULT ]]
then
# welcome message
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
# get total games played
GAMES_PLAYED=$($PSQL "SELECT * FROM guessing_game WHERE username = '$USERNAME' ORDER BY game_id DESC LIMIT 1")
# get score from best game played
BEST_GAME=$($PSQL "SELECT score FROM guessing_game WHERE username = '$USERNAME' ORDER BY score DESC LIMIT 1")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
echo "Guess the secret number between 1 and 1000:"
MAIN_GUESS
do you have more thane one database?
I noticed that in the screenshot you posted the dbname is different than in the code
I guess this one is another one? (maybe double check this one is okay also from the terminal)
PSQL=“psql -X --username=freecodecamp --dbname=number_guess --tuples-only -c”
I wish I was standing next to you to point it out…
But I’m not. You can see I was quoting your post because of the way the forum blocks out the quotes:
(the block starts with “kwame.arms:” which means that is from your post and there’s a tiny arrow pointer you can click that takes you to the specific post.)
Maybe your code doesn’t look like what you posted anymore?
Edit: you can also double check in the terminal by running a command like: grep number_guess number_guess.sh
if the grep finds the word it will print the occurrences out.
I am not sure that it is a connection error because you are able to connect on the command line. I mean there must be something in the script that is causing the issue.
Can I see your script again?