Number Guessing Game

I can’t pass the test but my code runs fine. Can someone give me advice on what could possibly be wrong with my code, thanks.

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

echo "Enter your username:"
read USER_NAME_INPUT

# random_num () {
#   r=$(( $RANDOM % 1001 ));
#   echo $r
# }

MY_NUM=$(( $RANDOM % 1001))
echo $MY_NUM

USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name='$USER_NAME_INPUT'")

if [[ -z $USER_ID ]]
then
  echo -e "\nWelcome, $USER_NAME_INPUT! It looks like this is your first time here."
  INSERT_USER_DATA=$($PSQL "INSERT INTO users(user_name) VALUES('$USER_NAME_INPUT')")
  
else
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id=$USER_ID")
  BEST_GAME=$($PSQL "SELECT min(tries) FROM games WHERE user_id=$USER_ID")
  echo "Welcome back, $USER_NAME_INPUT! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

TRIES=0

until [[ $GUESS -eq $MY_NUM ]]
do
  echo -e "\nGuess the secret number between 1 and 1000:"
  read GUESS

  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
    echo "That is not an integer, guess again:"
  else
  TRIES=$(( $TRIES+1 ))
    if [[ $GUESS -gt $MY_NUM ]]
    then
      echo "It's lower than that, guess again:"
    elif  [[ $GUESS -lt $MY_NUM ]]
    then
       echo "It's higher than that, guess again:"
    # else 
    #   break
    fi          
  fi
done

USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name='$USER_NAME_INPUT'")
INSERT_GAME_DATA=$($PSQL "INSERT INTO games(user_id, tries) VALUES($USER_ID, $TRIES)")


echo "You guessed it in $TRIES tries. The secret number was $MY_NUM. Nice job!"

Any particular test is giving you troubles? Could you share dump of you db as well?

Yes, I’ve run it several times for the last two days without changing the code and it’ll give me a fail different ones each time. I ran it six time and the problems were at:
8,9,13
8,13
7,8,9,10,12,13
7,8,13
8,11,12,13
8,10,13.

Below is the db dump:

--
-- 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_guess;
--
-- Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE number_guess OWNER TO freecodecamp;

\connect number_guess

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: games; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.games (
    game_id integer NOT NULL,
    user_id integer NOT NULL,
    tries integer NOT NULL
);


ALTER TABLE public.games OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.games_game_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;

--
-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.users (
    user_id integer NOT NULL,
    user_name character varying(22) NOT NULL
);


ALTER TABLE public.users OWNER TO freecodecamp;

--
-- Name: users_user_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.users_user_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.users_user_id_seq OWNER TO freecodecamp;

--
-- Name: users_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.users_user_id_seq OWNED BY public.users.user_id;


--
-- Name: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);


--
-- Name: users user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users ALTER COLUMN user_id SET DEFAULT nextval('public.users_user_id_seq'::regclass);


--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.games VALUES (1, 1, 509);
INSERT INTO public.games VALUES (2, 1, 851);
INSERT INTO public.games VALUES (3, 2, 708);
INSERT INTO public.games VALUES (4, 2, 894);
INSERT INTO public.games VALUES (5, 1, 627);
INSERT INTO public.games VALUES (6, 1, 868);
INSERT INTO public.games VALUES (7, 1, 689);
INSERT INTO public.games VALUES (8, 3, 5);
INSERT INTO public.games VALUES (9, 3, 5);
INSERT INTO public.games VALUES (10, 4, 5);


--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.users VALUES (1, 'user_1677739349244');
INSERT INTO public.users VALUES (2, 'user_1677739349243');
INSERT INTO public.users VALUES (3, 'charlie');
INSERT INTO public.users VALUES (4, 'larry');


--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.games_game_id_seq', 10, true);


--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.users_user_id_seq', 4, true);


--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);


--
-- Name: users users_user_name_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_user_name_key UNIQUE (user_name);


--
-- Name: games games_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete
--


Thanks, it seem the most troublesome is printing Guess the secret number between 1 and 1000: before each guess. That’s expected to be printed only at the start of game.

Test is determining number of guesses made and the secret number in a peculiar way, it basically makes new guess after every line is printed. Due to that it appears that test can miss the correct number, in a case when guessing it would correspond with line not expecting reading from input.

That was it. Moved that echo outside the loop and all passed. Thanks