Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I am stuck at task 8 and 13 in the Number Guessing Game project.

Your code so far

number_guess.sh:

#!/bin/bash

# init variables
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
RANDOM_NUMBER=$((1 + RANDOM % 1000))
GUESS_COUNT=0


while true
do
  # get username
  echo "Enter your username:"
  read USERNAME

  if [[ -z $USERNAME ]]
  then
    echo -e "Username invalid, must be 1+ letters long.\n"
  else
    break
  fi

done

# get user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")

# check if username have user_id
if [[ -z $USER_ID ]]
then
  # have not user_id, so create user
  $PSQL "INSERT INTO users(username) VALUES('$USERNAME');"
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
else
  # have user_id, so get asked for info
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID;")
  BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games WHERE user_id = $USER_ID;")
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# runs until break
while true
do
  #get user number guessed
  echo "Guess the secret number between 1 and 1000:"
  read GUESS

  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
    # if not valid, continue with whle (meaning go to start, cause no extra code after the if)
    echo "That is not an integer, guess again:"
    continue
  else
    #if valid, inc guess count
    GUESS_COUNT=$((GUESS_COUNT + 1))

    # if guessed correctly, give nessesary values, insert into games table this game and exit while loop
    if (( GUESS == RANDOM_NUMBER ))
    then
      echo "You guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!"
      $PSQL "INSERT INTO games(guesses,user_id) VALUES($GUESS_COUNT,$USER_ID);"
      break

      # If incorrect guess, respond accordingly
    elif (( GUESS > RANDOM_NUMBER ))
    then
        echo "It's lower than that, guess again:"
    else
        echo "It's higher than that, guess again:"
    fi
  fi


done

sql dump (single test run and 3 times i played):

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.22 (Ubuntu 12.22-0ubuntu0.20.04.4)
-- Dumped by pg_dump version 12.22 (Ubuntu 12.22-0ubuntu0.20.04.4)

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,
    guesses integer NOT NULL,
    user_id integer 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(30) 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 (707, 641, 521);
INSERT INTO public.games VALUES (708, 143, 521);
INSERT INTO public.games VALUES (709, 609, 522);
INSERT INTO public.games VALUES (710, 796, 522);
INSERT INTO public.games VALUES (711, 129, 521);
INSERT INTO public.games VALUES (712, 639, 521);
INSERT INTO public.games VALUES (713, 136, 521);
INSERT INTO public.games VALUES (714, 10, 523);
INSERT INTO public.games VALUES (715, 8, 523);
INSERT INTO public.games VALUES (716, 8, 523);


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

INSERT INTO public.users VALUES (521, 'user_1758540748408');
INSERT INTO public.users VALUES (522, 'user_1758540748407');
INSERT INTO public.users VALUES (523, 'ewie');


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

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


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

SELECT pg_catalog.setval('public.users_user_id_seq', 523, 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
--


when i played (please note, I removed the string between the @ and the : just for incase. ):

camper@:/workspace/project/number_guessing_game$ ./number_guess.sh
Enter your username:

Username invalid, must be 1+ letters long.

Enter your username:

ewie

INSERT 0 1
Welcome, ewie! It looks like this is your first time here.
Guess the secret number between 1 and 1000:

500

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

250

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

125

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

175 

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

200

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

225

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

235

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

230

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

227

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

228

You guessed it in 10 tries. The secret number was 228. Nice job!
INSERT 0 1

camper@:/workspace/project/number_guessing_game$ ./number_guess.sh
Enter your username:

ewie

Welcome back, ewie! You have played 1 games, and your best game took 10 guesses.
Guess the secret number between 1 and 1000:

500

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

250

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

125

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

185

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

215

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

225

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

235

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

245

You guessed it in 8 tries. The secret number was 245. Nice job!
INSERT 0 1

camper@:/workspace/project/number_guessing_game$ ./number_guess.sh
Enter your username:

ewie

Welcome back, ewie! You have played 2 games, and your best game took 8 guesses.
Guess the secret number between 1 and 1000:

fe

That is not an integer, guess again:
Guess the secret number between 1 and 1000:

qe

That is not an integer, guess again:
Guess the secret number between 1 and 1000:

-100

That is not an integer, guess again:
Guess the secret number between 1 and 1000:

10000

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

500

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

800

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

900

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

950

It's higher than that, guess again:
Guess the secret number between 1 and 1000:

970

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

960

It's lower than that, guess again:
Guess the secret number between 1 and 1000:

955

You guessed it in 8 tries. The secret number was 955. Nice job!
INSERT 0 1

output log:

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.<anonymous> (test/1.1.test.js:98:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  ✘ SUBTASKS 1.1 :13 Your script should print the correct message when a game is finished

AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert(re.test(scriptOutput))

at Context.<anonymous> (test/1.1.test.js:133:5)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I already tried:

project/.freeCodeCamp/test/utils.js (& 1.1.test.js)
Untitled

utils.js

project/package.jsonUntitled

but it didn’t work.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

You shouldn’t have anything like that in your output.

Thanks for the reply. I removed the insert01, but it did not fix the errors the test gives me.

updated code:

#!/bin/bash

# init variables
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
RANDOM_NUMBER=$((1 + RANDOM % 1000))
GUESS_COUNT=0


while true
do
  # get username
  echo "Enter your username:"
  read USERNAME

  if [[ -z $USERNAME ]]
  then
    echo -e "Username invalid, must be 1+ letters long.\n"
  else
    break
  fi

done

# get user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")

# check if username have user_id
if [[ -z $USER_ID ]]
then
  # have not user_id, so create user
  code=$($PSQL "INSERT INTO users(username) VALUES('$USERNAME');")
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
  echo "Welcome, $USERNAME! It looks like this is your first time here."
else
  # have user_id, so get asked for info
  GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games WHERE user_id = $USER_ID;")
  BEST_GAME=$($PSQL "SELECT MIN(guesses) FROM games WHERE user_id = $USER_ID;")
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# runs until break
while true
do
  #get user number guessed
  echo "Guess the secret number between 1 and 1000:"
  read GUESS

  if [[ ! $GUESS =~ ^[0-9]+$ ]]
  then
    # if not valid, continue with whle (meaning go to start, cause no extra code after the if)
    echo "That is not an integer, guess again:"
    continue
  else
    #if valid, inc guess count
    GUESS_COUNT=$((GUESS_COUNT + 1))

    # if guessed correctly, give nessesary values, insert into games table this game and exit while loop
    if (( GUESS == RANDOM_NUMBER ))
    then
      echo "You guessed it in $GUESS_COUNT tries. The secret number was $RANDOM_NUMBER. Nice job!"
      code=$($PSQL "INSERT INTO games(guesses,user_id) VALUES($GUESS_COUNT,$USER_ID);")
      break

      # If incorrect guess, respond accordingly
    elif (( GUESS > RANDOM_NUMBER ))
    then
        echo "It's lower than that, guess again:"
    else
        echo "It's higher than that, guess again:"
    fi
  fi


done

you’re expected to count all the guesses including the invalid ones. It doesn’t appear to be what you’re doing, so I’d fix that next.

also don’t add any code that was not requested. (I don’t recall a request to check for a valid username?)

Thanks for the help. after the 3rd time of running the updated code. It worked.

For anyone that may struggle with the same issue I struggled with, I leave my .sh here:

code removed by moderator

the sql you can find in the first comment.

it is not allowed to share solutions on the forum, I will be removing your .sh file