The Number Guessing Game has a faulty test

I was working on the the number guessing game and I noticed the test 1.13 never passes no matter what I do. I looked in the form for answers but nothing worked. I decided to look at the tests and found that test 1.13 checks for two variables in the string [guesses and (guesses - 1)]. I then made a variable that equals the (number of guesses - 1) and put it in the second part of the string instead of the secret number and the test passes. The same thing happens in gitpod and vscode+docker.

#!/bin/bash

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

SECRET_NUMBER=$(( $RANDOM % 1000 + 1 ))
NUMBER_OF_TURNS=$(( 1 ))
NUMBER=$(( 0 ))

echo "Enter your username:"
read USERNAME

GAMES_PLAYED=$($PSQL "select games_played from users where username = '$USERNAME'")

if [[ -z $GAMES_PLAYED ]]
then
    TEMP=$($PSQL "insert into users(username) values('$USERNAME')")

    echo "Welcome, $USERNAME! It looks like this is your first time here."
else
    BEST_GAME=$($PSQL "select best_game from users where username = '$USERNAME'")

    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi

(( GAMES_PLAYED += 1 ))

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

while (( $NUMBER != $SECRET_NUMBER ))
do
    (( NUMBER_OF_TURNS += 1 ))

    read NUMBER

    while [[ ! $NUMBER =~ ^[0-9]+$ ]]
    do
        echo "That is not an integer, guess again:"
        read NUMBER
    done

    if (( $NUMBER > $SECRET_NUMBER ))
    then
        echo "It's lower than that, guess again:"
    else
        echo "It's higher than that, guess again:"
    fi
done

TEST=$(( $NUMBER_OF_TURNS - 1 ))

echo "You guessed it in $NUMBER_OF_TURNS tries. The secret number was $TEST. Nice job!"

#echo "You guessed it in $NUMBER_OF_TURNS tries. The secret number was $SECRET_NUMBER. Nice job!"

if [[ -z $BEST_GAME || $NUMBER_OF_TURNS < $BEST_GAME ]]
then
    TEMP=$($PSQL "update users set best_game = $NUMBER_OF_TURNS, games_played = $GAMES_PLAYED where username = '$USERNAME'")
fi

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.19 (Ubuntu 12.19-0ubuntu0.20.04.1)
-- Dumped by pg_dump version 12.19 (Ubuntu 12.19-0ubuntu0.20.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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.users (
    username character varying(22) NOT NULL,
    games_played integer DEFAULT 0,
    best_game integer
);


ALTER TABLE public.users OWNER TO freecodecamp;

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

INSERT INTO public.users VALUES ('user_1725051568656', 1, 158);
INSERT INTO public.users VALUES ('user_1725051568657', 3, 23);
INSERT INTO public.users VALUES ('user_1725051571140', 1, 13);
INSERT INTO public.users VALUES ('user_1725051571139', 1, 19);
INSERT INTO public.users VALUES ('user_1725051573289', 1, 30);
INSERT INTO public.users VALUES ('user_1725051573290', 2, 206);
INSERT INTO public.users VALUES ('user_1725051575395', 2, 611);
INSERT INTO public.users VALUES ('user_1725051575394', 2, 659);
INSERT INTO public.users VALUES ('user_1725051577596', 1, 22);
INSERT INTO public.users VALUES ('user_1725051577597', 3, 499);
INSERT INTO public.users VALUES ('user_1725051579854', 1, 267);
INSERT INTO public.users VALUES ('user_1725051579855', 2, 101);
INSERT INTO public.users VALUES ('user_1725051582001', 2, 448);
INSERT INTO public.users VALUES ('user_1725051582002', 2, 202);
INSERT INTO public.users VALUES ('user_1725051584150', 2, 16);
INSERT INTO public.users VALUES ('user_1725051584149', 2, 778);
INSERT INTO public.users VALUES ('user_1725051672266', 1, 115);
INSERT INTO public.users VALUES ('user_1725051672265', 2, 377);
INSERT INTO public.users VALUES ('user_1725051674560', 2, 318);
INSERT INTO public.users VALUES ('user_1725051674559', 1, 1000);
INSERT INTO public.users VALUES ('user_1725051676945', 1, 116);
INSERT INTO public.users VALUES ('user_1725051676944', 2, 344);
INSERT INTO public.users VALUES ('user_1725051679151', 1, 384);
INSERT INTO public.users VALUES ('user_1725051679152', 3, 413);
INSERT INTO public.users VALUES ('user_1725051681525', 1, 302);
INSERT INTO public.users VALUES ('user_1725051681526', 3, 481);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_pkey PRIMARY KEY (username);


--
-- PostgreSQL database dump complete
--


Instead of doing this, change your code to store all the games in the table and then pick the one record with the minimum guesses each time an existing user plays: also use the same table to count how many times the person played before (store one record for each game).

That is, do not store the games played value or the best game value. Use the query to count the games played instead and find the best game.

Also in the code below:

You need to increment the number of guesses in the loop (that is, even bad guesses are guesses)

Finally, the last thing the program should do is output the success message. The call to update the table should be done before that as the test expects you to exit immediately after the last message.

The test Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses. passes with no problems. Its the You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job! test that has I have a problem with.

I tried counting bad guesses and moving the echo to the end of the code, but it still doesn’t pass. The weird thing is that when I print the massage with the NUMBER_OF_TURNS and SECRET_NUMBER variables the test doesn’t pass, but when I use the NUMBER_OF_TURNS and NUMBER_OF_TURNS - 1 variables it allows passes.

You are off by one because you started counting from 1. Try changing the initial number of turns to zero and then when this code runs it will increment it to one (but be careful with the bad guesses as they need to be counted too.

Also watch your logic. When the guess is correct, you must also add one.

Sorry for the late response. I managed to get the test to pass thanks to your feedback.

I initially managed to get it to pass by using

removed

As I mentioned in the original post. It worked every time I ran the tests. I don’t know if this is a bug or not.

But now it also passes using

removed

Thanks for your help!

Glad you got it, please don’t post solution code though, thanks :+1: