When i manually test the script the output seems to match those two tests but the tests don’t pass
- If that username has been used before, it should print
Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses.
, with<username>
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
-When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! and finish running
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=username -t --no-align -c"
echo "Enter your username:"
read username
namecheck=$($PSQL "select username from data where username='$username'")
if [[ -z $namecheck ]]
then
echo "Welcome, $username! It looks like this is your first time here."
$PSQL "insert into data(username) values('$username')"
userid=$($PSQL "select user_id from data where username='$username'")
else
userid=$($PSQL "select user_id from data where username='$username'")
games_played=$($PSQL "select count(game_id) from games where user_id=$userid" | tr -d ' ')
best_game=$($PSQL "select min(guesses) from games where user_id=$userid" | tr -d ' ')
echo "Welcome back, '$username'! You have played $games_played games, and your best game took $best_game guesses."
fi
echo "Guess the secret number between 1 and 1000:"
num=$(( RANDOM % 1000 + 1 ))
tries=0
while true
do
read guess
if [[ $guess =~ ^[0-9]+$ ]]
then
((tries++))
if [[ $guess -eq $num ]]
then
echo "You guessed it in $tries tries. The secret number was $num. Nice job!"
$PSQL "insert into games(user_id, guesses) values($userid, $tries)"
exit 0
elif [[ $guess -gt $num ]]
then
echo "It's lower than that, guess again:"
else
echo "It's higher than that, guess again:"
fi
else
echo "That is not an integer, guess again:"
fi
done
--
-- 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 username;
--
-- Name: username; Type: DATABASE; Schema: -; Owner: freecodecamp
--
CREATE DATABASE username WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';
ALTER DATABASE username OWNER TO freecodecamp;
\connect username
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: data; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.data (
user_id integer NOT NULL,
username character varying(22) NOT NULL
);
ALTER TABLE public.data OWNER TO freecodecamp;
--
-- Name: data_user_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--
CREATE SEQUENCE public.data_user_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.data_user_id_seq OWNER TO freecodecamp;
--
-- Name: data_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--
ALTER SEQUENCE public.data_user_id_seq OWNED BY public.data.user_id;
--
-- Name: games; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.games (
game_id integer NOT NULL,
user_id integer,
guesses integer NOT NULL,
player_at timestamp without time zone DEFAULT now()
);
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: data user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.data ALTER COLUMN user_id SET DEFAULT nextval('public.data_user_id_seq'::regclass);
--
-- 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);
--
-- Data for Name: data; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.data VALUES (1, 'user_1746280035395');
INSERT INTO public.data VALUES (2, 'user_1746280035394');
INSERT INTO public.data VALUES (3, 'Jabba');
INSERT INTO public.data VALUES (4, 'user_1746280255687');
INSERT INTO public.data VALUES (5, 'user_1746280255686');
INSERT INTO public.data VALUES (6, 'user_1746280407916');
INSERT INTO public.data VALUES (7, 'user_1746280407915');
INSERT INTO public.data VALUES (8, 'user_1746280410462');
INSERT INTO public.data VALUES (9, 'user_1746280410461');
INSERT INTO public.data VALUES (10, 'user_1746280412964');
INSERT INTO public.data VALUES (11, 'user_1746280412963');
INSERT INTO public.data VALUES (12, 'user_1746280415359');
INSERT INTO public.data VALUES (13, 'user_1746280415358');
INSERT INTO public.data VALUES (14, 'user_1746280481340');
INSERT INTO public.data VALUES (15, 'user_1746280481339');
--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.games VALUES (1, 1, 533, '2025-05-03 13:47:15.580256');
INSERT INTO public.games VALUES (2, 1, 508, '2025-05-03 13:47:15.809748');
INSERT INTO public.games VALUES (3, 2, 777, '2025-05-03 13:47:15.996051');
INSERT INTO public.games VALUES (4, 2, 289, '2025-05-03 13:47:16.171077');
INSERT INTO public.games VALUES (5, 1, 449, '2025-05-03 13:47:16.351071');
INSERT INTO public.games VALUES (6, 1, 465, '2025-05-03 13:47:16.542415');
INSERT INTO public.games VALUES (7, 1, 446, '2025-05-03 13:47:16.718987');
INSERT INTO public.games VALUES (8, 3, 10, '2025-05-03 13:47:55.105032');
INSERT INTO public.games VALUES (9, 4, 566, '2025-05-03 13:50:56.037524');
INSERT INTO public.games VALUES (10, 4, 922, '2025-05-03 13:50:56.371794');
INSERT INTO public.games VALUES (11, 5, 445, '2025-05-03 13:50:56.598533');
INSERT INTO public.games VALUES (12, 5, 171, '2025-05-03 13:50:56.808206');
INSERT INTO public.games VALUES (13, 4, 517, '2025-05-03 13:50:57.092141');
INSERT INTO public.games VALUES (14, 4, 971, '2025-05-03 13:50:57.433257');
INSERT INTO public.games VALUES (15, 4, 235, '2025-05-03 13:50:57.654452');
INSERT INTO public.games VALUES (16, 6, 938, '2025-05-03 13:53:28.125036');
INSERT INTO public.games VALUES (17, 6, 184, '2025-05-03 13:53:28.270292');
INSERT INTO public.games VALUES (18, 7, 129, '2025-05-03 13:53:28.394704');
INSERT INTO public.games VALUES (19, 7, 712, '2025-05-03 13:53:28.611928');
INSERT INTO public.games VALUES (20, 6, 584, '2025-05-03 13:53:28.807381');
INSERT INTO public.games VALUES (21, 6, 727, '2025-05-03 13:53:28.996044');
INSERT INTO public.games VALUES (22, 6, 928, '2025-05-03 13:53:29.217205');
INSERT INTO public.games VALUES (23, 8, 835, '2025-05-03 13:53:30.706283');
INSERT INTO public.games VALUES (24, 8, 649, '2025-05-03 13:53:30.945365');
INSERT INTO public.games VALUES (25, 9, 986, '2025-05-03 13:53:31.129132');
INSERT INTO public.games VALUES (26, 9, 222, '2025-05-03 13:53:31.281287');
INSERT INTO public.games VALUES (27, 8, 315, '2025-05-03 13:53:31.451103');
INSERT INTO public.games VALUES (28, 8, 802, '2025-05-03 13:53:31.640251');
INSERT INTO public.games VALUES (29, 8, 145, '2025-05-03 13:53:31.793613');
INSERT INTO public.games VALUES (30, 10, 83, '2025-05-03 13:53:33.089083');
INSERT INTO public.games VALUES (31, 10, 346, '2025-05-03 13:53:33.258343');
INSERT INTO public.games VALUES (32, 11, 486, '2025-05-03 13:53:33.405547');
INSERT INTO public.games VALUES (33, 11, 748, '2025-05-03 13:53:33.613663');
INSERT INTO public.games VALUES (34, 10, 804, '2025-05-03 13:53:33.837133');
INSERT INTO public.games VALUES (35, 10, 25, '2025-05-03 13:53:33.963159');
INSERT INTO public.games VALUES (36, 10, 509, '2025-05-03 13:53:34.132458');
INSERT INTO public.games VALUES (37, 12, 585, '2025-05-03 13:53:35.515049');
INSERT INTO public.games VALUES (38, 12, 805, '2025-05-03 13:53:35.716188');
INSERT INTO public.games VALUES (39, 13, 998, '2025-05-03 13:53:35.91224');
INSERT INTO public.games VALUES (40, 13, 417, '2025-05-03 13:53:36.068933');
INSERT INTO public.games VALUES (41, 12, 119, '2025-05-03 13:53:36.204185');
INSERT INTO public.games VALUES (42, 12, 317, '2025-05-03 13:53:36.363869');
INSERT INTO public.games VALUES (43, 12, 221, '2025-05-03 13:53:36.508147');
INSERT INTO public.games VALUES (44, 14, 518, '2025-05-03 13:54:41.487962');
INSERT INTO public.games VALUES (45, 14, 769, '2025-05-03 13:54:41.684922');
INSERT INTO public.games VALUES (46, 15, 700, '2025-05-03 13:54:41.878897');
INSERT INTO public.games VALUES (47, 15, 613, '2025-05-03 13:54:42.072174');
INSERT INTO public.games VALUES (48, 14, 306, '2025-05-03 13:54:42.233793');
INSERT INTO public.games VALUES (49, 14, 825, '2025-05-03 13:54:42.435206');
INSERT INTO public.games VALUES (50, 14, 262, '2025-05-03 13:54:42.583576');
--
-- Name: data_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.data_user_id_seq', 15, true);
--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.games_game_id_seq', 50, true);
--
-- Name: data data_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.data
ADD CONSTRAINT data_pkey PRIMARY KEY (user_id);
--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games
ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);
--
-- Name: data unq_name; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.data
ADD CONSTRAINT unq_name UNIQUE (username);
--
-- 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.data(user_id);
--
-- PostgreSQL database dump complete
--