Number Guessing Game - Build a Number Guessing Game

Tell us what’s happening:

I’m stuck with this, the test 8 and 13 don’t work. I check everything and i can’t see what’s wrong with the code. Please i need help, thank you.

Your code so far

This is mi database dump so you can see where i working on

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

CREATE TABLE public.games (
    game_id integer NOT NULL,
    user_id integer,
    guesses integer NOT NULL,
    played_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);


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(22) NOT NULL,
    best_game integer DEFAULT 0 NOT NULL,
    games_played integer DEFAULT 0 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 (1, 1, 12, '2025-04-11 02:17:55.858886');
INSERT INTO public.games VALUES (2, 2, 19, '2025-04-11 02:18:14.774475');
INSERT INTO public.games VALUES (3, 2, 859, '2025-04-11 02:18:14.933526');
INSERT INTO public.games VALUES (4, 3, 743, '2025-04-11 02:18:15.101632');
INSERT INTO public.games VALUES (5, 3, 52, '2025-04-11 02:18:15.204246');
INSERT INTO public.games VALUES (6, 2, 227, '2025-04-11 02:18:15.319104');
INSERT INTO public.games VALUES (7, 2, 664, '2025-04-11 02:18:15.446146');
INSERT INTO public.games VALUES (8, 2, 388, '2025-04-11 02:18:15.557087');
INSERT INTO public.games VALUES (9, 4, 636, '2025-04-11 02:24:36.644287');
INSERT INTO public.games VALUES (10, 4, 132, '2025-04-11 02:24:36.763405');
INSERT INTO public.games VALUES (11, 5, 287, '2025-04-11 02:24:36.902313');
INSERT INTO public.games VALUES (12, 5, 665, '2025-04-11 02:24:37.053117');
INSERT INTO public.games VALUES (13, 4, 244, '2025-04-11 02:24:37.186891');
INSERT INTO public.games VALUES (14, 4, 702, '2025-04-11 02:24:37.321997');
INSERT INTO public.games VALUES (15, 4, 369, '2025-04-11 02:24:37.453807');
INSERT INTO public.games VALUES (16, 6, 329, '2025-04-11 02:25:16.758879');
INSERT INTO public.games VALUES (17, 6, 24, '2025-04-11 02:25:16.863128');
INSERT INTO public.games VALUES (18, 7, 21, '2025-04-11 02:25:16.972609');
INSERT INTO public.games VALUES (19, 7, 274, '2025-04-11 02:25:17.087233');
INSERT INTO public.games VALUES (20, 6, 808, '2025-04-11 02:25:17.256246');
INSERT INTO public.games VALUES (21, 6, 649, '2025-04-11 02:25:17.414653');
INSERT INTO public.games VALUES (22, 6, 834, '2025-04-11 02:25:17.612667');
INSERT INTO public.games VALUES (23, 8, 11, '2025-04-11 02:28:29.422577');
INSERT INTO public.games VALUES (24, 8, 12, '2025-04-11 02:29:37.146524');
INSERT INTO public.games VALUES (25, 9, 811, '2025-04-11 02:29:50.558787');
INSERT INTO public.games VALUES (26, 9, 864, '2025-04-11 02:29:50.764004');
INSERT INTO public.games VALUES (27, 10, 255, '2025-04-11 02:29:50.921207');
INSERT INTO public.games VALUES (28, 10, 676, '2025-04-11 02:29:51.112295');
INSERT INTO public.games VALUES (29, 9, 140, '2025-04-11 02:29:51.2504');
INSERT INTO public.games VALUES (30, 9, 686, '2025-04-11 02:29:51.439497');
INSERT INTO public.games VALUES (31, 9, 773, '2025-04-11 02:29:51.623552');
INSERT INTO public.games VALUES (32, 11, 365, '2025-04-11 02:30:46.513489');
INSERT INTO public.games VALUES (33, 11, 699, '2025-04-11 02:30:46.651161');
INSERT INTO public.games VALUES (34, 12, 715, '2025-04-11 02:30:46.810662');
INSERT INTO public.games VALUES (35, 12, 360, '2025-04-11 02:30:46.931323');
INSERT INTO public.games VALUES (36, 11, 95, '2025-04-11 02:30:47.042972');
INSERT INTO public.games VALUES (37, 11, 384, '2025-04-11 02:30:47.16201');
INSERT INTO public.games VALUES (38, 11, 489, '2025-04-11 02:30:47.305069');


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

INSERT INTO public.users VALUES (1, 'Joaquin', 12, 1);
INSERT INTO public.users VALUES (3, 'user_1744337894666', 52, 2);
INSERT INTO public.users VALUES (2, 'user_1744337894667', 19, 5);
INSERT INTO public.users VALUES (5, 'user_1744338276474', 287, 2);
INSERT INTO public.users VALUES (4, 'user_1744338276475', 132, 5);
INSERT INTO public.users VALUES (7, 'user_1744338316617', 21, 2);
INSERT INTO public.users VALUES (6, 'user_1744338316618', 24, 5);
INSERT INTO public.users VALUES (8, 'Kiko', 11, 2);
INSERT INTO public.users VALUES (10, 'user_1744338590351', 255, 2);
INSERT INTO public.users VALUES (9, 'user_1744338590352', 140, 5);
INSERT INTO public.users VALUES (12, 'user_1744338646365', 360, 2);
INSERT INTO public.users VALUES (11, 'user_1744338646366', 95, 5);


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

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


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

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

and this is my script for the bash

#!/bin/bash

PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# Funcion para jugar al juego
play_game() {
  local USER_ID=$1
  # Crea el numero secreto
  SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
  GUESSES=0
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id = $USER_ID")

  echo "Guess the secret number between 1 and 1000:"

  while true; do
    read USER_GUESS

    # Verifica que se ingrese un numero
    if ! [[ "$USER_GUESS" =~ ^[0-9]+$ ]] || (( USER_GUESS < 1 || USER_GUESS > 1000 ));
    then
      echo "That is not an integer, guess again:"
      continue
    fi

    GUESSES=$((GUESSES + 1))
    # Verifica si el numero es menor o mayor
    if (( USER_GUESS < SECRET_NUMBER ));
    then
      echo "It's higher than that, guess again:"
    elif (( USER_GUESS > SECRET_NUMBER ));
    then
      echo "It's lower than that, guess again:"
    else
      #Suma un juego mas a la cantidad de veces jugadas
      GAMES_PLAYED=$((GAMES_PLAYED + 1))
      #Verifica si ha sido su mejor juego y si lo es lo registra en la base de datos
      if [[ $BEST_GAME -eq 0 || $GUESSES -lt $BEST_GAME ]];
      then
        $PSQL "UPDATE users SET best_game = $GUESSES, games_played = $GAMES_PLAYED WHERE user_id = $USER_ID;"
      else
        #En caso de que no haya sido su mejor juego, solo regista la suma del total de juegos jugados 
       $PSQL "UPDATE users SET games_played = $GAMES_PLAYED WHERE user_id = $USER_ID;"
      fi
      # Ingresa el resultado a la base de datos y felicita al usuario por adivinar el numero
      $PSQL "INSERT INTO games(user_id, guesses) VALUES ($USER_ID, $GUESSES);" >/dev/null
      echo "You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
      exit 0
    fi
  done
  stop
}

# Solicita ingresar el nombre al usuario
echo "Enter your username:"
read USERNAME

# Obtiene la informacion del usuario
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
# Verifica si existe el usuario
if [[ -z "$USER_ID" ]]; then
  # Si el usuario es nuevo saluda y lo registra en la base de datos
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "INSERT INTO users(username) VALUES ('$USERNAME');" >/dev/null
  USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME';")
else
  # En caso de que el usuario exista obtiene las estadisticas de la base de datos
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id = $USER_ID;")
  BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id = $USER_ID;")

  # Si no jugó ningún juego, devuelve por default 0
  if [[ -z "$BEST_GAME" || "$BEST_GAME" == "NULL" ]]; then
    BEST_GAME=0
  fi

  # Saludo al usuario y devuelve sus estadísticas de partidas anteriores
  echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

# Corre el juego con la información del usuario
play_game "$USER_ID"

Soo i don’t see why don’t pass the tests I double chequed and still.
Someone pls help me.. :sob:

Your browser information:

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

Challenge Information:

Number Guessing Game - Build a Number Guessing Game

Hi @joaquinlallana12

Can you post the text for tests 8 and 13?

Happy coding

This is the test 8

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

And this is the test 13

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

Hi @joaquinlallana12

Should the welcome message appear before asking the player to guess a number?

Happy coding

where are you counting these guesses?