Hello,
I am having trouble getting my code to pass.
Story 16 never passes, but the others sometimes to and sometimes don’t, even if I don’t change anything.
Here is my code:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
SECRET_NUMBER=$(( $RANDOM % 1000 + 1 ))
TRIES=1
GUESS=0
GAME() {
echo "Enter your username:"
read USERNAME
RETURNING_USER=$($PSQL "SELECT username FROM users WHERE username = '$USERNAME'")
if [[ -z $RETURNING_USER ]]
then
INSERT_USER=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME')")
echo "Welcome, $USERNAME! It looks like this is your first time here."
else
GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games INNER JOIN users USING(user_id) WHERE username = '$USERNAME'")
BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games INNER JOIN users USING(user_id) WHERE username = '$USERNAME'")
echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
#grab user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")
#echo $SECRET_NUMBER
echo "Guess the secret number between 1 and 1000:"
read GUESS
while [[ ! $GUESS = $SECRET_NUMBER ]]
do
if [[ $GUESS =~ ^[0-9]+$ ]]
then
if [[ $GUESS -lt $SECRET_NUMBER ]]
#input is lower than secret number
then
echo "It's higher than that, guess again:"
read GUESS
TRIES=$(( $TRIES + 1 ))
elif [[ $GUESS -gt $SECRET_NUMBER ]]
#input is higher than secret number
then
echo "It's lower than that, guess again:"
read GUESS
TRIES=$(( $TRIES + 1 ))
fi
fi
if [[ ! $GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
read GUESS
fi
done
#elif [[ $GUESS = $SECRET_NUMBER ]]
#then
if [[ $GUESS = $SECRET_NUMBER ]]
then
echo "You guessed it in $TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
INSERTED_INTO_GAMESr=$($PSQL "INSERT INTO games(user_id, guesses) VALUES($USER_ID, $TRIES)")
GUESS=1
fi
}
GAME
SQL 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,
guesses integer DEFAULT 0 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,
username character varying(25) 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 (498, 113, 11);
INSERT INTO public.games VALUES (499, 114, 876);
INSERT INTO public.games VALUES (500, 114, 209);
INSERT INTO public.games VALUES (501, 115, 349);
INSERT INTO public.games VALUES (502, 115, 858);
INSERT INTO public.games VALUES (503, 114, 762);
INSERT INTO public.games VALUES (504, 114, 786);
INSERT INTO public.games VALUES (505, 114, 216);
INSERT INTO public.games VALUES (506, 116, 891);
INSERT INTO public.games VALUES (507, 116, 444);
INSERT INTO public.games VALUES (508, 117, 554);
INSERT INTO public.games VALUES (509, 117, 666);
INSERT INTO public.games VALUES (510, 116, 864);
INSERT INTO public.games VALUES (511, 116, 891);
INSERT INTO public.games VALUES (512, 116, 984);
INSERT INTO public.games VALUES (513, 118, 347);
INSERT INTO public.games VALUES (514, 119, 108);
INSERT INTO public.games VALUES (515, 119, 312);
INSERT INTO public.games VALUES (516, 118, 477);
INSERT INTO public.games VALUES (517, 118, 526);
INSERT INTO public.games VALUES (518, 118, 280);
INSERT INTO public.games VALUES (519, 120, 396);
INSERT INTO public.games VALUES (520, 120, 343);
INSERT INTO public.games VALUES (521, 121, 876);
INSERT INTO public.games VALUES (522, 121, 822);
INSERT INTO public.games VALUES (523, 120, 732);
INSERT INTO public.games VALUES (524, 120, 44);
INSERT INTO public.games VALUES (525, 120, 359);
INSERT INTO public.games VALUES (526, 122, 992);
INSERT INTO public.games VALUES (527, 123, 423);
INSERT INTO public.games VALUES (528, 123, 726);
INSERT INTO public.games VALUES (529, 122, 144);
INSERT INTO public.games VALUES (530, 122, 608);
INSERT INTO public.games VALUES (531, 122, 958);
INSERT INTO public.games VALUES (532, 124, 458);
INSERT INTO public.games VALUES (533, 124, 710);
INSERT INTO public.games VALUES (534, 125, 556);
INSERT INTO public.games VALUES (535, 125, 794);
INSERT INTO public.games VALUES (536, 124, 903);
INSERT INTO public.games VALUES (537, 124, 403);
INSERT INTO public.games VALUES (538, 124, 228);
INSERT INTO public.games VALUES (539, 126, 347);
INSERT INTO public.games VALUES (540, 126, 852);
INSERT INTO public.games VALUES (541, 127, 711);
INSERT INTO public.games VALUES (542, 127, 213);
INSERT INTO public.games VALUES (543, 126, 293);
INSERT INTO public.games VALUES (544, 126, 867);
INSERT INTO public.games VALUES (545, 126, 353);
INSERT INTO public.games VALUES (546, 128, 175);
INSERT INTO public.games VALUES (547, 129, 26);
INSERT INTO public.games VALUES (548, 129, 198);
INSERT INTO public.games VALUES (549, 128, 902);
INSERT INTO public.games VALUES (550, 128, 747);
INSERT INTO public.games VALUES (551, 128, 347);
INSERT INTO public.games VALUES (552, 130, 663);
INSERT INTO public.games VALUES (553, 131, 788);
INSERT INTO public.games VALUES (554, 131, 834);
INSERT INTO public.games VALUES (555, 130, 757);
INSERT INTO public.games VALUES (556, 130, 754);
INSERT INTO public.games VALUES (557, 130, 404);
INSERT INTO public.games VALUES (558, 132, 576);
INSERT INTO public.games VALUES (559, 132, 875);
INSERT INTO public.games VALUES (560, 133, 857);
INSERT INTO public.games VALUES (561, 132, 455);
INSERT INTO public.games VALUES (562, 132, 755);
INSERT INTO public.games VALUES (563, 132, 243);
--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.users VALUES (113, 'Stephanie');
INSERT INTO public.users VALUES (114, 'user_1680199047389');
INSERT INTO public.users VALUES (115, 'user_1680199047387');
INSERT INTO public.users VALUES (116, 'user_1680199084387');
INSERT INTO public.users VALUES (117, 'user_1680199084386');
INSERT INTO public.users VALUES (118, 'user_1680199194313');
INSERT INTO public.users VALUES (119, 'user_1680199194312');
INSERT INTO public.users VALUES (120, 'user_1680199254208');
INSERT INTO public.users VALUES (121, 'user_1680199254207');
INSERT INTO public.users VALUES (122, 'user_1680199279413');
INSERT INTO public.users VALUES (123, 'user_1680199279412');
INSERT INTO public.users VALUES (124, 'user_1680199328971');
INSERT INTO public.users VALUES (125, 'user_1680199328970');
INSERT INTO public.users VALUES (126, 'user_1680199351818');
INSERT INTO public.users VALUES (127, 'user_1680199351817');
INSERT INTO public.users VALUES (128, 'user_1680199359636');
INSERT INTO public.users VALUES (129, 'user_1680199359635');
INSERT INTO public.users VALUES (130, 'user_1680199381869');
INSERT INTO public.users VALUES (131, 'user_1680199381868');
INSERT INTO public.users VALUES (132, 'user_1680199395930');
INSERT INTO public.users VALUES (133, 'user_1680199395929');
--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.games_game_id_seq', 563, true);
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 133, 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_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_username_key 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.users(user_id);
--
-- PostgreSQL database dump complete
--
I have read other forum post and I still can’t seem to get it to pass.
Thanks in advance!