Build a Number Guessing game

I haven’t been able to get the 8th or 13th test to pass- been cracking it around for four or so hours trying different advice from this forum but so far nothing has seemed effective. I’m incrementing the number of guesses only on integers and starting at 1 to account for the correct guess, I’m engaging with the database only using variables, and as far as I can see I don’t have any typos or formatting errors to cause these tests to fail.

I think it must be something related to the number of guesses value, because that’s the only thing in common between the two tests. When I run the script myself it always lines up with how many guesses it took and the output matches the test. Will include my script and sql dump below - any ideas would be appreciated!


My number_guess.sh file:

#! /bin/bash

PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -c”
re=‘[1]+$’
LOGIN() {
echo “Enter your username:”
read USER_NAME
NAME_CHECK=$($PSQL “SELECT username FROM numbers_guess WHERE username=‘$USER_NAME’”)

if [[ -z $NAME_CHECK ]]
then
INSERT_USER=$($PSQL “INSERT INTO numbers_guess(username) VALUES(‘$USER_NAME’)”)
NAME_CHECK=$($PSQL “SELECT username FROM numbers_guess WHERE username=‘$USER_NAME’”)
echo “Welcome, $NAME_CHECK! It looks like this is your first time here.”
else
TOTAL_CHECK=$($PSQL “SELECT games_played FROM numbers_guess WHERE username=‘$NAME_CHECK’”)
BEST_CHECK=$($PSQL “SELECT best_game FROM numbers_guess WHERE username=‘$NAME_CHECK’”)
echo “Welcome back, $NAME_CHECK! You have played $TOTAL_CHECK games, and your best game took $BEST_CHECK guesses.”
fi
}
LOGIN

GAME_START() {
echo “Guess the secret number between 1 and 1000:”
RAND_NUM=$(( $RANDOM%1000+1 ))
I=0
GAME_LOOP() {
read GUESS
if [[ ! $GUESS =~ $re || $GUESS -gt 1000 || $GUESS -lt 1 ]]
then
echo “That is not an integer, guess again:”
GAME_LOOP
else
if [[ $GUESS -eq $RAND_NUM ]]
then
I=$(( I+1 ))
echo “You guessed it in $I tries. The secret number was $GUESS. Nice job!”
if [[ $I -lt $BEST_CHECK || -z $BEST_CHECK ]]
then
INSERT_BEST=$($PSQL “UPDATE numbers_guess SET best_game=‘$I’ WHERE username=‘$USER_NAME’”)
fi
TOTAL_CHECK=$(( $TOTAL_CHECK+1 ))
INSERT_TOTAL=$($PSQL “UPDATE numbers_guess SET games_played=‘$TOTAL_CHECK’ WHERE username=‘$USER_NAME’”)
echo “Thanks for playing.”
elif [[ $GUESS -gt $RAND_NUM ]]
then
echo “It’s lower than that, guess again:”
I=$(( I+1 ))
GAME_LOOP
else
echo “It’s higher than that, guess again:”
I=$(( I+1 ))
GAME_LOOP
fi
fi
}
GAME_LOOP
}
GAME_START


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

CREATE TABLE public.numbers_guess (

games_played integer,

best_game integer,

username character varying(25)

);

ALTER TABLE public.numbers_guess OWNER TO freecodecamp;

– Data for Name: numbers_guess; Type: TABLE DATA; Schema: public; Owner: freecodecamp

INSERT INTO public.numbers_guess VALUES (2, 84, ‘user_1679130581786’);

INSERT INTO public.numbers_guess VALUES (2, 143, ‘user_1679130206225’);

INSERT INTO public.numbers_guess VALUES (5, 5, ‘user_1679130581787’);

INSERT INTO public.numbers_guess VALUES (5, 338, ‘user_1679130206226’);

INSERT INTO public.numbers_guess VALUES (2, 738, ‘user_1679175145640’);

INSERT INTO public.numbers_guess VALUES (2, 271, ‘user_1679130685901’);

INSERT INTO public.numbers_guess VALUES (2, 80, ‘user_1679130246677’);

INSERT INTO public.numbers_guess VALUES (5, 102, ‘user_1679130246678’);

INSERT INTO public.numbers_guess VALUES (2, 94, ‘user_1679170245205’);

INSERT INTO public.numbers_guess VALUES (2, 254, ‘user_1679130587452’);

INSERT INTO public.numbers_guess VALUES (5, 38, ‘user_1679130685902’);

INSERT INTO public.numbers_guess VALUES (5, 4, ‘user_1679130587453’);

INSERT INTO public.numbers_guess VALUES (2, 438, ‘user_1679130374719’);

INSERT INTO public.numbers_guess VALUES (5, 181, ‘user_1679170245206’);

INSERT INTO public.numbers_guess VALUES (5, 116, ‘user_1679130374720’);

INSERT INTO public.numbers_guess VALUES (2, 513, ‘user_1679130592581’);

INSERT INTO public.numbers_guess VALUES (2, 124, ‘user_1679172641896’);

INSERT INTO public.numbers_guess VALUES (1, 504, ‘user_1679130541246’);

INSERT INTO public.numbers_guess VALUES (5, 129, ‘user_1679175145641’);

INSERT INTO public.numbers_guess VALUES (2, 605, ‘user_1679130691253’);

INSERT INTO public.numbers_guess VALUES (5, 221, ‘user_1679130592582’);

INSERT INTO public.numbers_guess VALUES (5, 23, ‘user_1679130541247’);

INSERT INTO public.numbers_guess VALUES (5, 326, ‘user_1679130691254’);

INSERT INTO public.numbers_guess VALUES (2, 10, ‘user_1679130551673’);

INSERT INTO public.numbers_guess VALUES (2, 50, ‘user_1679130598314’);

INSERT INTO public.numbers_guess VALUES (5, 494, ‘user_1679172641897’);

INSERT INTO public.numbers_guess VALUES (5, 89, ‘user_1679130551674’);

INSERT INTO public.numbers_guess VALUES (2, 181, ‘user_1679170298410’);

INSERT INTO public.numbers_guess VALUES (5, 277, ‘user_1679130598315’);

INSERT INTO public.numbers_guess VALUES (2, 171, ‘user_1679130558028’);

INSERT INTO public.numbers_guess VALUES (2, 74, ‘user_1679130696820’);

INSERT INTO public.numbers_guess VALUES (5, 597, ‘user_1679130558029’);

INSERT INTO public.numbers_guess VALUES (5, 5, ‘user_1679170298411’);

INSERT INTO public.numbers_guess VALUES (2, 566, ‘user_1679130562932’);

INSERT INTO public.numbers_guess VALUES (2, 278, ‘user_1679130603292’);

INSERT INTO public.numbers_guess VALUES (4, 496, ‘user_1679130696821’);

INSERT INTO public.numbers_guess VALUES (5, 44, ‘user_1679130562933’);

INSERT INTO public.numbers_guess VALUES (5, 213, ‘user_1679130603293’);

INSERT INTO public.numbers_guess VALUES (2, 203, ‘user_1679130567335’);

INSERT INTO public.numbers_guess VALUES (5, 23, ‘user_1679130567336’);

INSERT INTO public.numbers_guess VALUES (2, 320, ‘user_1679172728720’);

INSERT INTO public.numbers_guess VALUES (2, 239, ‘user_1679130610783’);

INSERT INTO public.numbers_guess VALUES (2, 377, ‘user_1679130708600’);

INSERT INTO public.numbers_guess VALUES (2, 519, ‘user_1679175474066’);

INSERT INTO public.numbers_guess VALUES (5, 47, ‘user_1679130610784’);

INSERT INTO public.numbers_guess VALUES (2, 216, ‘user_1679130573364’);

INSERT INTO public.numbers_guess VALUES (2, 427, ‘user_1679170442430’);

INSERT INTO public.numbers_guess VALUES (5, 107, ‘user_1679130573365’);

INSERT INTO public.numbers_guess VALUES (5, 363, ‘user_1679130708601’);

INSERT INTO public.numbers_guess VALUES (5, 83, ‘user_1679172728721’);

INSERT INTO public.numbers_guess VALUES (2, 290, ‘user_1679130616321’);

INSERT INTO public.numbers_guess VALUES (5, 392, ‘user_1679170442431’);

INSERT INTO public.numbers_guess VALUES (2, 858, ‘user_1679130576915’);

INSERT INTO public.numbers_guess VALUES (5, 80, ‘user_1679175474067’);

INSERT INTO public.numbers_guess VALUES (5, 308, ‘user_1679130616322’);

INSERT INTO public.numbers_guess VALUES (5, 33, ‘user_1679130576916’);

INSERT INTO public.numbers_guess VALUES (3, 11, ‘griff’);

INSERT INTO public.numbers_guess VALUES (2, 202, ‘user_1679130888266’);

INSERT INTO public.numbers_guess VALUES (5, 58, ‘user_1679130888267’);

INSERT INTO public.numbers_guess VALUES (2, 713, ‘user_1679170557933’);

INSERT INTO public.numbers_guess VALUES (2, 884, ‘user_1679169884648’);

INSERT INTO public.numbers_guess VALUES (5, 71, ‘user_1679170557934’);

INSERT INTO public.numbers_guess VALUES (5, 195, ‘user_1679169884649’);

INSERT INTO public.numbers_guess VALUES (2, 73, ‘user_1679174515568’);

INSERT INTO public.numbers_guess VALUES (2, 345, ‘user_1679171090056’);

INSERT INTO public.numbers_guess VALUES (2, 355, ‘user_1679169962438’);

INSERT INTO public.numbers_guess VALUES (4, 342, ‘user_1679171090057’);

INSERT INTO public.numbers_guess VALUES (5, 126, ‘user_1679169962439’);

INSERT INTO public.numbers_guess VALUES (5, 697, ‘user_1679174515569’);

INSERT INTO public.numbers_guess VALUES (2, 292, ‘user_1679172394949’);

INSERT INTO public.numbers_guess VALUES (5, 8, ‘user_1679172394950’);

INSERT INTO public.numbers_guess VALUES (2, 556, ‘user_1679174925804’);

INSERT INTO public.numbers_guess VALUES (5, 220, ‘user_1679174925805’);

– Name: numbers_guess numbers_guess_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp

ALTER TABLE ONLY public.numbers_guess

ADD CONSTRAINT numbers_guess_username_key UNIQUE (username);

– PostgreSQL database dump complete


Let me know if there’s anything else I should share re: my project. Thanks for your time.


  1. 0-9 ↩︎