Number Guessing Game - failing Existing Username test

Hello coders,

I’ve been struggling with this one for hours. Some of the tests fail sometimes, but pass most of the times, but this particular test fails EVERYTIME even though the output is exactly what the test expects (see attached screenshot).

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
I’ve read through lots of posts of people who have experienced the same issue. Couldn’t find a solution for my problem, but it makes me think there might actually be something wrong with the way the tests are run

What is probably an issue on my end, though, is a very weird bug the root of which I cannot pinpoint: random users get added to the users table for some reason. Sometimes it happens when I reload the container; I check the table and two rows have automaticlly filled, as if the script had been playing by itself in the background (again, please see attached picture). You guess is as good as mine here (actually, it’s probably better). Don’t know if it’s relevant, but the first versions of my script ran with functions that called themselves when the user input wasn’t valid, but that resulted in problematic recursiveness so I got rid of them 100% (took me a little, but eventually remembered I was supposed to use while loops for that). To me, this sounds ludacris, but is it possible that they’re somehow still running in the background?
[EDIT: day-long brainfart over. I’ve finally realized that those “random users” are being added by the tests, like the instructions very clearly explain beforehand]

Here’s the code for my bash script. I strive to add comments that will make my code easy to follow, so I’ll be interested if you have any feedback on that as well!

#! /bin/bash
# --- PSQL VARIABLE FOR QUERYING DATABASE
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# --- ASK FOR NAME
# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
echo "Enter your username:"
read USER
# --- IF NAME IS GREATER THAN 22 CHAR, OR EMPTY:
while [[ ${#USER} -gt 22 || -z $USER ]]
do
  echo "Too long. Enter a username that's 22-characters long or less."
  read USER
done
# --- ONCE NAME IS VALID, CHECK IF IT ALREADY EXISTS
# --- FETCH USER NAME
USERNAME=$($PSQL "SELECT username FROM users WHERE username LIKE '$USER'")
# --- IF NAME ALREADY EXISTS:
if [[ $USERNAME ]]
then
  # --- FETCH GAME DATA
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username LIKE '$USER'")
  BEST_SCORE=$($PSQL "SELECT best_game FROM users WHERE username LIKE '$USER'")
  # --- "WELCOME BACK" MESSAGE
  echo -e "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_SCORE guesses."
# --- IF NAME IS VALID AND NEW:
elif [[ -z $USERNAME ]]
then
# --- ADD USER
  echo $($PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$USER', 0, 0);")
  echo "Welcome, $USER! It looks like this is your first time here."
fi
# --- S T A R T   G A M E
# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
# --- CREATE NUMBER
NUMBER=$((RANDOM % 1001))
ATTEMPTS=0
# --- ASK USER TO GUESS
echo "Guess the secret number between 1 and 1000:"
# --- GET USER INPUT
read USER_GUESS
  # --- IF INPUT IS INCORRECT :
  while [[ $USER_GUESS != $NUMBER ]]
  do
  ((ATTEMPTS+=1))
  # --- IF INPUT IS NOT AN INTEGER:
    if [[ ! $USER_GUESS =~ ^[0-9]+$ ]]
    then
      # ((ATTEMPTS+=1))
      echo "That is not an integer, guess again:"
      read USER_GUESS
  # --- IF INPUT IS HIGHER:
    elif [[ $USER_GUESS -gt $NUMBER ]]
    then
      # ((ATTEMPTS+=1))
      echo "It's lower than that, guess again:"
      read USER_GUESS
  # --- IF INPUT IS LOWER:
    elif [[ $USER_GUESS -lt $NUMBER ]]
    then
      # ((ATTEMPTS+=1))
      echo "It's higher than that, guess again:"
      read USER_GUESS
    fi
  done
  # --- IF INPUT IS RIGHT:
  if [[ $USER_GUESS -eq $NUMBER ]]
  then
    ((ATTEMPTS+=1))
    # --- CHECK IF INPUT IS LOWER THAN BEST SCORE, OR IF IT'S THE FIRST GAME:
    if [[ $ATTEMPTS -lt $BEST_SCORE || $BEST_SCORE -eq 0 ]]
    # --- IF LOWER, OR FIRST GAME, ADD TO DATABASE
    then
      echo $($PSQL "UPDATE users SET best_game=$ATTEMPTS WHERE username='$USER'")
    fi  
    # --- INCREASE GAME COUNT
    ((GAMES_PLAYED++))
    echo $($PSQL "UPDATE users SET games_played=$GAMES_PLAYED WHERE username='$USER'")
    # --- ENDING MESSAGE
    echo "You guessed it in $ATTEMPTS tries. The secret number was $NUMBER. Nice job!"
  fi

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

CREATE TABLE public.users (
    username character varying(22) NOT NULL,
    games_played integer,
    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_1707702503777', 2, 151);
INSERT INTO public.users VALUES ('user_1707702503778', 5, 118);
INSERT INTO public.users VALUES ('user_1707702812093', 2, 481);
INSERT INTO public.users VALUES ('user_1707702812094', 5, 12);


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

ALTER TABLE ONLY public.users
    ADD CONSTRAINT users_username_key UNIQUE (username);


--
-- PostgreSQL database dump complete
--


As always, thanks in advance to everyone who takes the time to read my post!

UPDATE: I completely deleted the entire coderoad and started all over again. Obviously copied and pasted the bash script, hoping that would take care of any residual issues that might have been left from previous versions of the script.

UPDATE 2: OK, I figured it out. I will come back to this message later and explain what was failing in case anyone experiences the same issue in the future, but for now, I don’t want to waste anybody’s time should they try to help.

Nothing. I cannot get that test to pass, even though the output is exactly as expected, and those random users keep getting added to the table [EDIT: day-long brainfart over. I’ve finally realized that those “random users” are being added by the tests, like the instructions very clearly explain beforehand].
Stick Guy Slamming His Head On Computer
Still don’t understand why the test isn’t passing though. Could someone please check (or lead me to, if possible) the repo of the tests so I can see exactly what it’s looking for? There’s always the possibility that something was left out of the instructions (at this point, I don’t know what else could be the problem, at least in regards to that particular test). Thanks in advance.

I took a look - try removing the printing of the query responses in the script. e.g. when the script prints INSERT 0 1 or UPDATE 1, etc. That got everything to pass for me.

1 Like

Thank you moT01! You saved me from further embarrassment because I thought the issue lied somewhere else, lol. Indeed, the issue was the echo before the queries for inserting or updating data on the tables, which was causing the script to output “INSERT 0 1” (or something like that) along with the correct message, therefore rendering the output incorrect, as it was supposed to output the required message only.

1 Like

Thanks for reporting, you weren’t the only one. I updated the instructions to better clarify what the script should print.