Number Guessing Game // SUBTASKS 1.1 :8

Working on this project and this last task is having trouble. If anyone is able to spot my mistake, please let me know. I looked at previous posts but couldn’t find a solution that worked for me.

The task:
If that username has been used before, it should print

Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses., with being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game

Code for number_guess.sh

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# START
echo -e "Enter your username:"
read USERNAME

# Get the name_id
NAME_ID=$($PSQL "SELECT name_id FROM names WHERE name='$USERNAME'")

# Check to see if username exists
if [[ -z $NAME_ID ]]
  # If name id does not show up, then print welcome message and insert new data
  then
  echo -e "Welcome, $USERNAME! It looks like this is your first time here."
  INSERT_NAME=$($PSQL "INSERT INTO names(name) VALUES ('$USERNAME')")
  # If name id exists, then print the stats
  else
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE name_id=$NAME_ID")
  BEST_GAME=$($PSQL "SELECT MIN(number_guess) FROM games WHERE name_id=$NAME_ID")
  echo -e "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi


# Create the secret number
SECRET_NUMBER=$(($RANDOM % 1000))
echo -e "Guess the secret number between 1 and 1000:"

#Start guessing
COUNTS=$1 #Number of guesses
#while case is true
while [[ "$test_case"=true ]]
do
  # Get an input
  read GUESS
  COUNTS=$((COUNTS+1))
#if guess is not integer, then guess again
  if [[ -n ${GUESS//[0-9]/} ]]
  then
  echo -e "That is not an integer, guess again:"
  else
    #else if guess matches, then case is false to exit loop
    if [[ $GUESS -eq $SECRET_NUMBER ]]
    then
      break
    else
      #else if guess is larger, then counts+=1
      if [[ $GUESS -gt $SECRET_NUMBER ]]
      then
      echo -e "It's lower than that, guess again:"
      else
        #else if guess if smaller, then counts+=1
        if [[ $GUESS -lt $SECRET_NUMBER ]]
        then
        echo -e "It's higher than that, guess again:"
        fi
      fi
    fi
  fi
done

# Guessed correctly, then print statement and input data
INSERT_DATA=$($PSQL "INSERT INTO games (name_id, number_guess) VALUES ($NAME_ID, $COUNTS)")
echo -e "You guessed it in $COUNTS tries. The secret number was $SECRET_NUMBER. Nice job!"

SQL dump

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.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,
    name_id integer,
    number_guess integer
);


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

CREATE TABLE public.names (
    name_id integer NOT NULL,
    name character varying(22) NOT NULL
);


ALTER TABLE public.names OWNER TO freecodecamp;

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

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


ALTER TABLE public.names_name_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.names_name_id_seq OWNED BY public.names.name_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: names name_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.names ALTER COLUMN name_id SET DEFAULT nextval('public.names_name_id_seq'::regclass);


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

INSERT INTO public.games VALUES (1, 11, 923);
INSERT INTO public.games VALUES (2, 12, 864);
INSERT INTO public.games VALUES (3, 11, 717);
INSERT INTO public.games VALUES (4, 11, 240);
INSERT INTO public.games VALUES (5, 11, 963);
INSERT INTO public.games VALUES (6, 13, 253);
INSERT INTO public.games VALUES (7, 14, 922);
INSERT INTO public.games VALUES (8, 13, 275);
INSERT INTO public.games VALUES (9, 13, 771);
INSERT INTO public.games VALUES (10, 13, 974);
INSERT INTO public.games VALUES (11, 15, 317);
INSERT INTO public.games VALUES (12, 16, 154);
INSERT INTO public.games VALUES (13, 15, 285);
INSERT INTO public.games VALUES (14, 15, 572);
INSERT INTO public.games VALUES (15, 15, 393);
INSERT INTO public.games VALUES (16, 17, 587);
INSERT INTO public.games VALUES (17, 18, 425);
INSERT INTO public.games VALUES (18, 17, 33);
INSERT INTO public.games VALUES (19, 17, 707);
INSERT INTO public.games VALUES (20, 17, 980);
INSERT INTO public.games VALUES (21, 19, 780);
INSERT INTO public.games VALUES (22, 20, 55);
INSERT INTO public.games VALUES (23, 19, 516);
INSERT INTO public.games VALUES (24, 19, 712);
INSERT INTO public.games VALUES (25, 19, 851);
INSERT INTO public.games VALUES (26, 21, 384);
INSERT INTO public.games VALUES (27, 22, 549);
INSERT INTO public.games VALUES (28, 21, 34);
INSERT INTO public.games VALUES (29, 21, 991);
INSERT INTO public.games VALUES (30, 21, 136);
INSERT INTO public.games VALUES (31, 23, 485);
INSERT INTO public.games VALUES (32, 24, 630);
INSERT INTO public.games VALUES (33, 23, 991);
INSERT INTO public.games VALUES (34, 23, 393);
INSERT INTO public.games VALUES (35, 23, 579);
INSERT INTO public.games VALUES (36, 25, 537);
INSERT INTO public.games VALUES (37, 26, 638);
INSERT INTO public.games VALUES (38, 25, 767);
INSERT INTO public.games VALUES (39, 25, 869);
INSERT INTO public.games VALUES (40, 25, 98);
INSERT INTO public.games VALUES (41, 27, 40);
INSERT INTO public.games VALUES (42, 28, 433);
INSERT INTO public.games VALUES (43, 27, 515);
INSERT INTO public.games VALUES (44, 27, 304);
INSERT INTO public.games VALUES (45, 27, 588);
INSERT INTO public.games VALUES (46, 8, 8);


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

INSERT INTO public.names VALUES (1, 'user_1718117989378');
INSERT INTO public.names VALUES (2, 'user_1718117989377');
INSERT INTO public.names VALUES (3, 'user_1718118037206');
INSERT INTO public.names VALUES (4, 'user_1718118037205');
INSERT INTO public.names VALUES (5, 'user_1718119109622');
INSERT INTO public.names VALUES (6, 'user_1718119109621');
INSERT INTO public.names VALUES (7, 'user_1718119865570');
INSERT INTO public.names VALUES (8, 'user_1718119865569');
INSERT INTO public.names VALUES (9, 'user_1718119938367');
INSERT INTO public.names VALUES (10, 'user_1718119938366');
INSERT INTO public.names VALUES (11, 'user_1718137668368');
INSERT INTO public.names VALUES (12, 'user_1718137668367');
INSERT INTO public.names VALUES (13, 'user_1718137674042');
INSERT INTO public.names VALUES (14, 'user_1718137674041');
INSERT INTO public.names VALUES (15, 'user_1718137682838');
INSERT INTO public.names VALUES (16, 'user_1718137682837');
INSERT INTO public.names VALUES (17, 'user_1718137688364');
INSERT INTO public.names VALUES (18, 'user_1718137688363');
INSERT INTO public.names VALUES (19, 'user_1718137782250');
INSERT INTO public.names VALUES (20, 'user_1718137782249');
INSERT INTO public.names VALUES (21, 'user_1718137804407');
INSERT INTO public.names VALUES (22, 'user_1718137804406');
INSERT INTO public.names VALUES (23, 'user_1718137865128');
INSERT INTO public.names VALUES (24, 'user_1718137865127');
INSERT INTO public.names VALUES (25, 'user_1718138213349');
INSERT INTO public.names VALUES (26, 'user_1718138213348');
INSERT INTO public.names VALUES (27, 'user_1718138260323');
INSERT INTO public.names VALUES (28, 'user_1718138260322');


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

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


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

SELECT pg_catalog.setval('public.names_name_id_seq', 28, 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: names names_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.names
    ADD CONSTRAINT names_pkey PRIMARY KEY (name_id);


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

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_name_id_fkey FOREIGN KEY (name_id) REFERENCES public.names(name_id);


--
-- PostgreSQL database dump complete
--


Edit: My outputs

ERROR: syntax error at or near “,”
LINE 1: INSERT INTO games (name_id, number_guess) VALUES (, 5)

$NAME_ID is empty when playing your first game as a new user.

Thanks! I see what I did wrong now… forgot to extract new ID for new usernames. Thank you again

1 Like