Relational Database - Number Guessing Game - Test 8 Not Passing No Matter What

I’ve been stuck for a few hours trying to make it work. I don’t think is on my end now because sometimes Test 13 fails even though I haven’t done anything.

In the forum there’s like 5 post of people running into similar issues with the tests in these project although nothing they did works or applies to me.

Here’s my code:

#!/bin/bash

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

NUMBER=$((RANDOM % 1000 + 1))
NUMBER_OF_GUESSES=0
GAMES_PLAYED=0

GUESS_LOOP() {
read GUESS

if [[ $GUESS =~ ^[0-9]+$ ]]
then
  if [[ $GUESS = $NUMBER ]]
  then
    let NUMBER_OF_GUESSES++

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

    #Update best game yet
    BEST_GAME=$($PSQL "SELECT best_game FROM guesser WHERE username = '$USERNAME'")
     if [[ $BEST_GAME -eq 0 || $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
    then
      UPDATE_BEST_GAME_RESULT=$($PSQL "UPDATE guesser SET best_game = '$NUMBER_OF_GUESSES' WHERE username = '$USERNAME'")
    fi
      
  else if [[ $GUESS > $NUMBER ]]
    then
      let NUMBER_OF_GUESSES++
      echo "It's lower than that, guess again:"
      GUESS_LOOP
    else 
      let NUMBER_OF_GUESSES++
      echo "It's higher than that, guess again:"
      GUESS_LOOP
    fi
  fi
else
  echo "That is not an integer, guess again:"
  GUESS_LOOP
fi
}

echo "Enter your username:"
read USERNAME

GUESSER=$($PSQL "SELECT * FROM guesser WHERE username = '$USERNAME'")

if [[ -z $GUESSER ]]
then
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  INSERT_USER_RESULT=$($PSQL "INSERT INTO guesser(username, games_played, best_game) VALUES('$USERNAME', 1, 0)")
else
  echo "$GUESSER" | while IFS="|" read USERNAME GAMES_PLAYED BEST_GAME
  do
    echo "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  done  
fi

#Update number of games played
GAMES_PLAYED=$($PSQL "SELECT games_played FROM guesser WHERE username = '$USERNAME'")
let GAMES_PLAYED++   
UPDATE_GAMES_PLAYED_RESULT=$($PSQL "UPDATE guesser SET games_played = '$GAMES_PLAYED' WHERE username = '$USERNAME'")

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

GUESS_LOOP

Here’s the 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: guesser; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.guesser (
    username character varying(16) NOT NULL,
    games_played integer,
    best_game integer
);


ALTER TABLE public.guesser OWNER TO freecodecamp;

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



--
-- Name: guesser guesser_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.guesser
    ADD CONSTRAINT guesser_username_key UNIQUE (username);


--
-- PostgreSQL database dump complete
--


I tried moving around where does the games_played variable gets updated if at the end of the game or after entering the username but it doesn’t seem to make a difference.

Hope someone can help me.

Could you post dump of the db as well?

I added the dump.

I also checked other people’s code on GitHub about the project and they are all doing similar things. I don’t see why mine’s not working.

I reset the project and tried again copying my code into it and get the same result. I think there’s a problem with the test, is in beta after all. Just hope it gets fixed.

Generally I’m also getting mixed results for the test 13, most of them passing though.

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!

Test fails because of the difference between expected number of tries and number of tries printed out. However I’m not sure where exactly that difference comes from.

Hmm. For that test I initialize the variable outside the guess loop and then increase after each try. I don’t store it in the database. When I run program, the number coincides with the number of time I’m asked for a number.

For you test 8 is just fine?

For that test, take a look at the initial number of games, when user is created.

I don’t see any problem with that so far. The number always corresponds to the number of games played.

The thing I noticed is that one can update that number at any time. The instructions don’t define when the games happens if at the end or at the beginning.

I also noticed that the instructions say the test will create a test user and to be free to delete those but so far I haven’t seen any user created by the tests.

Maybe that helps figure out the problem.

Edit: I just realized the only test that fails is the one that access the database so I think there something there. Maybe the test doesn’t have permission to access it. Is there a way for me to enable that?

There is a slight bug in the tests or description here @OphanimTV. The tests create a larger username than your table allows, you couldn’t have known that was happening. I changed the username column to varchar(25) to pass that part of it. One other thing I had to change to pass that test…

^ after playing one game, the database showed that I played two. I fixed that by changing one character in your script. Do those two things and you should be able to pass :+1: Let us know if you are still having issues.

It worked!!!

I had set my username to varchar 16, the project doesn’t specify length. It never occurred to me that could be the problem.

Thank you very much. Finished the project and got my certification!

1 Like

Awesome, congratulations :tada: if you share it on twitter I will give it a retweet :wink:

I also already added a sentence in the instructions to specify that length :+1:

How did you solve this? Am struggling for 5 hours now

Hi @moT01, it’s me again. There’s something fishy with the tests 1.8 and 1.13. Sometimes they pass, and sometimes they don’t. One can pass while the other fails. It looks random.

I did many tests in the terminal, refactored again and again, commented out my validity tests like I did to pass some of the tests from the salon scheduler… When I understood that they would pass or fail without me changing anything to my code, I just rolled the dice a few times until I got to the victory screen. Maybe it has something to do with the randomized users that are added for the tests?

Here’s a link to my solution that works (and then doesn’t… and then does again…).

And just to brag a bit, here’s a link to my newly-obtained certification in relational databases!

2 Likes

Hey y’all, I ran into some buggy behavior on the tests as well. Both 1.8, 1.13 as well as several others. Same thing–I could hit RUN multiple times and some tests would pass while others didn’t. And it would change each time.

I refactored my solution into one function and it passed fine. I originally had a choppy number_guess.sh file program structure where most of the solution was inside a GUESSING function, but then some pieces like the username prompt and the final database inserts were outside of it.

In practice, the program worked as it should have even like that, but the tests wouldn’t pass.

Once I moved everything except for the random number, guesses and psql variables inside that GUESSING function, all worked fine.