Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

Key Issue:

Relational Database: Number Guessing Game, SUBTASKS 1.1 :8 failed

Comment:

I’ve tried everything I can think of to solve the issue, but still failed. I have no clue of what blocks this subtasks… Thanks in advance

FAILED TEST LOG

✘ SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(re.test(scriptOutput))

at Context. (test/1.1.test.js:98:5)

Your code so far

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

START_GAME(){
	# Pick the secret number
	SECRET_NUM=$((RANDOM % 1000 + 1))  
	echo "Enter your username:"
	read USERNAME
	TOTAL_GUESS_NUM=0
	
	# Check if the username exists in the database
	TUN=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")
	TUSER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")  
	
	# If the username is not in the table
	if [[ -z $TUN ]]
	then
		echo "Welcome, $USERNAME! It looks like this is your first time here."
		# Insert new user to the table & create game_id for the user
		INSERT_NEW_USER=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME');")
		TUSER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
		TUN=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME';")
		CREATE_GID=$($PSQL "INSERT INTO game(user_id) VALUES($TUSER_ID);")
		NEW_GID=$($PSQL "SELECT MAX(game_id) FROM game WHERE user_id = $TUSER_ID;")
		GUESS_GAME "Guess the secret number between 1 and 1000:"
	
	# If the username exists in the table
	else
		# Create game_id for the user
		CREATE_GID=$($PSQL "INSERT INTO game(user_id) VALUES($TUSER_ID);")
		NEW_GID=$($PSQL "SELECT MAX(game_id) FROM game WHERE user_id = $TUSER_ID;")
		# Get the total games played and the least guess_num of one game
 		GAMES_PLAYED=$($PSQL "SELECT COUNT(game_id) FROM game WHERE user_id = $TUSER_ID;") 
		BEST_GAME=$($PSQL "SELECT MIN(guess_num) FROM game WHERE user_id =$TUSER_ID;") 
		echo "Welcome back, $TUN! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
		GUESS_GAME "Guess the secret number between 1 and 1000:"
  fi
}

GUESS_GAME(){
	((TOTAL_GUESS_NUM++))
	if [[ $1 ]]
	then 
		echo "$1"
	fi
	read GNUM
	CHECK_NUM
}

CHECK_NUM(){			
	# If the guess number is not a number 
	if ! [[ $GNUM =~ ^[0-9]+$ ]]
	then
		GUESS_GAME "That is not an integer, guess again:"
	
	# If the guess number is higher than the secret number
	elif [[ $GNUM -gt $SECRET_NUM ]]
	then
		GUESS_GAME "It's lower than that, guess again:"
	
	# If the guess number is lower than the secret number
	elif [[ $GNUM -lt $SECRET_NUM ]]
	then
		GUESS_GAME "It's higher than that, guess again:"
	
	# Guess the secret number -eq scenario
	else  
		INSERT_TOTAL_GNUM=$($PSQL "UPDATE game SET guess_num = $TOTAL_GUESS_NUM WHERE game_id = $NEW_GID;")
		echo "You guessed it in $TOTAL_GUESS_NUM tries. The secret number was $SECRET_NUM. Nice job!"
	fi
}

# Run the script
START_GAME

SQL

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

CREATE TABLE public.game (
    game_id integer NOT NULL,
    user_id integer,
    guess_num integer
);


ALTER TABLE public.game OWNER TO freecodecamp;

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

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


ALTER TABLE public.game_game_id_seq OWNER TO freecodecamp;

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

ALTER SEQUENCE public.game_game_id_seq OWNED BY public.game.game_id;


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

CREATE TABLE public.users (
    user_id integer NOT NULL,
    username character varying(30)
);


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: game game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.game ALTER COLUMN game_id SET DEFAULT nextval('public.game_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: game; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.game VALUES (1, 1, 11);
INSERT INTO public.game VALUES (61, 16, 804);
INSERT INTO public.game VALUES (7, 1, 4);
INSERT INTO public.game VALUES (8, 2, 2);
INSERT INTO public.game VALUES (9, 2, 11);
INSERT INTO public.game VALUES (10, 3, 2);
INSERT INTO public.game VALUES (11, 3, 5);
INSERT INTO public.game VALUES (12, 2, 5);
INSERT INTO public.game VALUES (13, 2, 4);
INSERT INTO public.game VALUES (14, 2, 7);
INSERT INTO public.game VALUES (15, 4, 11);
INSERT INTO public.game VALUES (16, 4, 11);
INSERT INTO public.game VALUES (17, 5, 9);
INSERT INTO public.game VALUES (18, 5, 10);
INSERT INTO public.game VALUES (19, 4, 6);
INSERT INTO public.game VALUES (20, 4, 5);
INSERT INTO public.game VALUES (21, 4, 8);
INSERT INTO public.game VALUES (22, 1, 4);
INSERT INTO public.game VALUES (23, 6, 836);
INSERT INTO public.game VALUES (24, 6, 555);
INSERT INTO public.game VALUES (25, 7, 113);
INSERT INTO public.game VALUES (26, 7, 465);
INSERT INTO public.game VALUES (27, 6, 882);
INSERT INTO public.game VALUES (28, 6, 419);
INSERT INTO public.game VALUES (29, 6, 947);
INSERT INTO public.game VALUES (30, 8, 882);
INSERT INTO public.game VALUES (31, 8, 846);
INSERT INTO public.game VALUES (32, 9, 889);
INSERT INTO public.game VALUES (33, 9, 749);
INSERT INTO public.game VALUES (34, 8, 452);
INSERT INTO public.game VALUES (35, 8, 481);
INSERT INTO public.game VALUES (36, 8, 857);
INSERT INTO public.game VALUES (37, 10, 715);
INSERT INTO public.game VALUES (38, 10, 147);
INSERT INTO public.game VALUES (39, 11, 422);
INSERT INTO public.game VALUES (40, 11, 249);
INSERT INTO public.game VALUES (41, 10, 791);
INSERT INTO public.game VALUES (42, 10, 76);
INSERT INTO public.game VALUES (43, 10, 980);
INSERT INTO public.game VALUES (44, 12, 106);
INSERT INTO public.game VALUES (45, 12, 858);
INSERT INTO public.game VALUES (46, 13, 179);
INSERT INTO public.game VALUES (47, 13, 25);
INSERT INTO public.game VALUES (48, 12, 61);
INSERT INTO public.game VALUES (49, 12, 39);
INSERT INTO public.game VALUES (50, 12, 757);
INSERT INTO public.game VALUES (51, 1, 9);
INSERT INTO public.game VALUES (52, 14, 33);
INSERT INTO public.game VALUES (53, 14, 15);
INSERT INTO public.game VALUES (54, 15, 448);
INSERT INTO public.game VALUES (55, 15, 892);
INSERT INTO public.game VALUES (56, 14, 105);
INSERT INTO public.game VALUES (57, 14, 712);
INSERT INTO public.game VALUES (58, 14, 192);
INSERT INTO public.game VALUES (62, 16, 242);
INSERT INTO public.game VALUES (63, 17, 354);
INSERT INTO public.game VALUES (64, 17, 717);
INSERT INTO public.game VALUES (65, 16, 712);
INSERT INTO public.game VALUES (66, 16, 176);
INSERT INTO public.game VALUES (67, 16, 231);
INSERT INTO public.game VALUES (68, 18, 254);
INSERT INTO public.game VALUES (70, 19, 246);
INSERT INTO public.game VALUES (75, 20, 923);
INSERT INTO public.game VALUES (76, 20, 293);
INSERT INTO public.game VALUES (77, 21, 445);
INSERT INTO public.game VALUES (78, 21, 292);
INSERT INTO public.game VALUES (79, 20, 898);
INSERT INTO public.game VALUES (80, 20, 21);
INSERT INTO public.game VALUES (81, 20, 977);
INSERT INTO public.game VALUES (82, 1, 10);
INSERT INTO public.game VALUES (83, 22, 12);
INSERT INTO public.game VALUES (84, 23, 424);
INSERT INTO public.game VALUES (85, 23, 53);
INSERT INTO public.game VALUES (86, 24, 649);
INSERT INTO public.game VALUES (87, 24, 704);
INSERT INTO public.game VALUES (88, 23, 600);
INSERT INTO public.game VALUES (89, 23, 526);
INSERT INTO public.game VALUES (90, 23, 758);
INSERT INTO public.game VALUES (91, 25, 770);
INSERT INTO public.game VALUES (92, 25, 709);
INSERT INTO public.game VALUES (93, 26, 53);
INSERT INTO public.game VALUES (94, 26, 639);
INSERT INTO public.game VALUES (95, 25, 670);
INSERT INTO public.game VALUES (96, 25, 20);
INSERT INTO public.game VALUES (97, 25, 553);
INSERT INTO public.game VALUES (98, 27, 660);
INSERT INTO public.game VALUES (99, 27, 127);
INSERT INTO public.game VALUES (100, 28, 542);
INSERT INTO public.game VALUES (101, 28, 353);
INSERT INTO public.game VALUES (102, 27, 314);
INSERT INTO public.game VALUES (103, 27, 576);
INSERT INTO public.game VALUES (104, 27, 652);
INSERT INTO public.game VALUES (105, 29, 187);
INSERT INTO public.game VALUES (106, 29, 754);
INSERT INTO public.game VALUES (107, 30, 543);
INSERT INTO public.game VALUES (108, 30, 56);
INSERT INTO public.game VALUES (109, 29, 249);
INSERT INTO public.game VALUES (110, 29, 596);
INSERT INTO public.game VALUES (111, 29, 296);
INSERT INTO public.game VALUES (112, 31, 135);
INSERT INTO public.game VALUES (113, 31, 96);
INSERT INTO public.game VALUES (114, 32, 584);
INSERT INTO public.game VALUES (115, 32, 502);
INSERT INTO public.game VALUES (116, 31, 33);
INSERT INTO public.game VALUES (117, 31, 572);
INSERT INTO public.game VALUES (118, 31, 979);
INSERT INTO public.game VALUES (119, 33, 447);
INSERT INTO public.game VALUES (120, 33, 602);
INSERT INTO public.game VALUES (121, 34, 562);
INSERT INTO public.game VALUES (122, 34, 61);
INSERT INTO public.game VALUES (123, 33, 938);
INSERT INTO public.game VALUES (124, 33, 262);
INSERT INTO public.game VALUES (125, 33, 778);
INSERT INTO public.game VALUES (156, 1, NULL);


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

INSERT INTO public.users VALUES (1, 'Maude');
INSERT INTO public.users VALUES (2, 'user_1749535583653');
INSERT INTO public.users VALUES (3, 'user_1749535583652');
INSERT INTO public.users VALUES (4, 'user_1749535685051');
INSERT INTO public.users VALUES (5, 'user_1749535685050');
INSERT INTO public.users VALUES (6, 'user_1749535938668');
INSERT INTO public.users VALUES (7, 'user_1749535938667');
INSERT INTO public.users VALUES (8, 'user_1749535984651');
INSERT INTO public.users VALUES (9, 'user_1749535984650');
INSERT INTO public.users VALUES (10, 'user_1749536128234');
INSERT INTO public.users VALUES (11, 'user_1749536128233');
INSERT INTO public.users VALUES (12, 'user_1749536243648');
INSERT INTO public.users VALUES (13, 'user_1749536243647');
INSERT INTO public.users VALUES (14, 'user_1749536573946');
INSERT INTO public.users VALUES (15, 'user_1749536573945');
INSERT INTO public.users VALUES (16, 'user_1749536763139');
INSERT INTO public.users VALUES (17, 'user_1749536763138');
INSERT INTO public.users VALUES (18, 'user_1749537119043');
INSERT INTO public.users VALUES (19, 'user_1749537119042');
INSERT INTO public.users VALUES (20, 'user_1749537227993');
INSERT INTO public.users VALUES (21, 'user_1749537227992');
INSERT INTO public.users VALUES (22, 'Nic');
INSERT INTO public.users VALUES (23, 'user_1749537635470');
INSERT INTO public.users VALUES (24, 'user_1749537635469');
INSERT INTO public.users VALUES (25, 'user_1749537647203');
INSERT INTO public.users VALUES (26, 'user_1749537647202');
INSERT INTO public.users VALUES (27, 'user_1749537676047');
INSERT INTO public.users VALUES (28, 'user_1749537676046');
INSERT INTO public.users VALUES (29, 'user_1749538343020');
INSERT INTO public.users VALUES (30, 'user_1749538343019');
INSERT INTO public.users VALUES (31, 'user_1749538423160');
INSERT INTO public.users VALUES (32, 'user_1749538423159');
INSERT INTO public.users VALUES (33, 'user_1749538473467');
INSERT INTO public.users VALUES (34, 'user_1749538473466');
INSERT INTO public.users VALUES (35, 'user_1749538828210');
INSERT INTO public.users VALUES (36, 'user_1749538828209');
INSERT INTO public.users VALUES (37, 'user_1749538879147');
INSERT INTO public.users VALUES (38, 'user_1749538879146');
INSERT INTO public.users VALUES (39, 'user_1749539016310');
INSERT INTO public.users VALUES (40, 'user_1749539016309');
INSERT INTO public.users VALUES (41, 'user_1749539195348');
INSERT INTO public.users VALUES (42, 'user_1749539195347');


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

SELECT pg_catalog.setval('public.game_game_id_seq', 156, true);


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

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


--
-- Name: game game_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.game
    ADD CONSTRAINT game_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: game game_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.game
    ADD CONSTRAINT game_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);


--
-- PostgreSQL database dump complete
--

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Welcome to the forum @maudes

Try removing the quote marks from the echo

Happy coding

Thanks Teller!
I just found the real issue of “the wrong $GAME_PLAYED output”.
I should echo the message first and generate the new game _id.

1 Like