Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Hello, I am working on the “Build a Number Guessing Game” course within the Relational Databases curriculum.

I am receiving an error stating “syntax error at end of input” and I am not sure why. I have placed single quotes around $USER_ID in the select statement and have removed them. Neither seem to work.

If I run the same query in the terminal, I do not receive an error. Any ideas as to why this is throwing the error?

The error is in a red box below. The line of code that this pertains to has a red arrow pointing towards it.

Thank you.

Your code so far

#! /bin/bash

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

#Random Number Generation

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

echo $NUMBER

#Guess Function

GUESS1() {

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

CURRENT_GAME=$($PSQL “SELECT COUNT(game_id) FROM games WHERE user_id = ‘$USER_ID’”)

TOTAL_GAMES= $CURRENT_GAME + 1

INSERT_GAME=$($PSQL “INSERT INTO games(game,user_id) VALUES(‘$TOTAL_GAMES, $USER_ID’)”)

GAME_COUNT=$($PSQL “SELECT MAX(game) FROM games WHERE user_id=$USER_ID”)

GAME_ID=$($PSQL “SELECT game_id FROM games WHERE user_id = ‘$USER_ID’ and game = ‘$GAME_COUNT’”)

}

#Number_Guess_Function

NUMBER_GUESS_FUNCTION() {

read NUMBER_GUESSED

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

then

GUESS_NOT_INT=$($PSQL “INSERT INTO guesses(guess,game_id) VALUES(‘$NUMBER_GUESSED’, $GAME_ID)”)

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

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED > $NUMBER ]];

then

GUESS_LOWER=$($PSQL “INSERT INTO guesses(guess,game_id) VALUES(‘$NUMBER_GUESSED’, $GAME_ID)”)

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

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED < $NUMBER ]];

then

GUESS_HIGHER=$($PSQL “INSERT INTO guesses(guess,game_id) VALUES(‘$NUMBER_GUESSED’, $GAME_ID)”)

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

NUMBER_GUESS_FUNCTION

elif [[ $NUMBER_GUESSED = $NUMBER ]];

then

GUESS_CORRECT=$($PSQL “INSERT INTO guesses(guess,game_id) VALUES(‘$NUMBER_GUESSED’, $GAME_ID)”)

GUESS_COUNT=$($PSQL “SELECT COUNT(guess_id) FROM guesses WHERE game_id=$GAME_ID”)

WIN_GUESS_COUNT=$($PSQL “INSERT INTO wins(guess_count,game_id) VALUES($GUESS_COUNT, $GAME_ID)”)

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

fi

}

#Username Question

USERNAME_QUESTION=“Enter your username:”

echo $USERNAME_QUESTION

read USERNAME_INPUT

#Variables

USER_ID=$($PSQL “SELECT user_id FROM username WHERE username = ‘$USERNAME_INPUT’”)

GAMES_PLAYED=$($PSQL"SELECT COUNT(game_id) FROM games WHERE user_id=$USER_ID")

LOWEST_GUESSES=$($PSQL"SELECT MIN(guess_count) FROM wins FULL JOIN games ON games.game_id = wins.game_id

FULL JOIN username ON username.user_id = games.user_id WHERE username= $USERNAME_INPUT")

USERNAME_DATABASE=$($PSQL “SELECT * FROM username WHERE username = ‘$USERNAME_INPUT’”)

#Username Database

if [[ -z $USERNAME_DATABASE ]];

then

INSERT_USERNAME=$($PSQL “INSERT INTO username(username) VALUES(‘$USERNAME_INPUT’)”)

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

else

echo "Welcome back, $USERNAME_INPUT! You have played $GAMES_PLAYED games,

and your best game took $LOWEST_GUESSES guesses."

fi

#functions in use

GUESS1

NUMBER_GUESS_FUNCTION

until [[ $NUMBER_GUESSED = $NUMBER ]]

do

NUMBER_GUESS_FUNCTION

done

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Hi @codecampacct

Make sure your code is using single quote marks around every variable.

Happy coding

Hello Teller, thank you for the response. I tried that and now I am receiving the error “invalid input syntax for type integer:”

Hi @codecampacct

It looks like you are using different variables for the username.

See if you can use just one variable.

Happy coding

Thank you for the response. I was able to get the error to go away. It seemed to have something to do with the position of my USER_ID variable. I moved it to a different location in the code and it worked. I believe my USER_ID variable was not returning a value due to the position in the code, which through the error.

1 Like

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