#!/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
Also, there are 6 other forum topics referencing the same error that were posted since August. Unfortunately, I can’t find CodeAlly support…
I’ve deleted the container one more time, just for piece of mind. Still having the issues so I dug into the code further. I’ve made some progress in understanding this issue…
If I remove, or comment, these lines the errors don’t show up
#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)")
However, I’ve used similar lines of code in the other projects from this course. I’m once again lost on how to fix this.
I am glad you did that because that is what I would have done also (it is a technique I use when things don’t make any sense to try to block lines out till I have a section of code to focus on)
my understanding is that because you are calling psql that it is attempting the select (regardless of whether you use the result in the script or not).
What changes if you fix that line?
Also you can try taking the select statements out and running them one by one manually with the current variables all replaced with their values at that moment.
(so instead of trying to run the select, you can echo the select statements out then try to run them)
if [[ $GUESS == $SECRET_NUMBER ]]
then
echo -e "Games played: $($PSQL "SELECT game_id FROM guessing_game WHERE username = 'Test2' ORDER BY game_id DESC LIMIT 1")"
echo -e "Best game: $($PSQL "SELECT score FROM guessing_game WHERE username = 'Test2' ORDER BY score DESC LIMIT 1")"
ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('Test2', $NUMBER_OF_TRIES)")
echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
else
inside the loop, there is another shell
That shell doesn’t have a connection to the database.
try connecting to the database inside the loop (not great i know but just a test)
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
DROP DATABASE number_guessing_game;
--
-- Name: number_guessing_game; Type: DATABASE; Schema: -; Owner: freecodecamp
--
CREATE DATABASE number_guessing_game WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';
ALTER DATABASE number_guessing_game OWNER TO freecodecamp;
\connect number_guessing_game
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: guessing_game; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.guessing_game (
game_id integer NOT NULL,
username character varying(22) NOT NULL,
score integer NOT NULL
);
ALTER TABLE public.guessing_game OWNER TO freecodecamp;
--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--
CREATE SEQUENCE public.guessing_game_game_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.guessing_game_game_id_seq OWNER TO freecodecamp;
--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--
ALTER SEQUENCE public.guessing_game_game_id_seq OWNED BY public.guessing_game.game_id;
--
-- Name: guessing_game game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.guessing_game ALTER COLUMN game_id SET DEFAULT nextval('public.guessing_game_game_id_seq'::regclass);
--
-- Data for Name: guessing_game; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.guessing_game VALUES (1, 'Test1', 15);
INSERT INTO public.guessing_game VALUES (2, 'Test2', 6);
INSERT INTO public.guessing_game VALUES (3, 'Test1', 12);
INSERT INTO public.guessing_game VALUES (4, 'Test2', 19);
--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.guessing_game_game_id_seq', 4, true);
--
-- PostgreSQL database dump complete
--
Script:
#!/bin/bash
# Number Guessing Game
PSQL="psql -X --username=freecodecamp --dbname=number_guessing_game -t --no-align -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
echo $($PSQL "SELECT * FROM guessing_game")
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