Number guessing game tests 8 and 13 won't pass

Hey folks,

I’m hungry for this certificate, it’s been a long and trouble full road going through it. I would highly appreciate if someone could spare a minute to go over my code and see why I can’t pass these two tricky tests I’ve been trying to pass for hours.

The program functions as expected and I can see the test users in PSQL afterwards, but these two tests never seem to pass.

Here is my number_guess.sh:

#!/bin/bash

PSQL="psql -X --username=freecodecamp --dbname=users --no-align --tuples-only -c"

NUMBER=$(( $RANDOM % 1000 + 1 ))

echo -e "\nEnter your username:"
read USERNAME

USERID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")

if [[ ! -z $USERID ]] 
then
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE username='$USERNAME'")
  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."
else
  echo "Welcome, $USERNAME! It looks like this is your first time here."
  $PSQL "INSERT INTO users(username) VALUES('$USERNAME')" | echo 
  GAMES_PLAYED=0
  BEST_GAME=0
fi
echo -e "\nGuess the secret number between 1 and 1000:"

GUESSES() {
  read GUESS
  COUNTER=$1

  if [[ $GUESS =~ ^[0-9]+$ ]]
  then
    if [[ $GUESS = $NUMBER ]]
    then 
      echo -e "You guessed it in $COUNTER tries. The secret number was $GUESS. Nice job!"
      $PSQL "UPDATE users SET games_played='$(( $GAMES_PLAYED + 1 ))' WHERE username='$USERNAME'" | echo
      if [[ $COUNTER < $BEST_GAME || $BEST_GAME = 0 ]]
      then
        $PSQL "UPDATE users SET best_game='$COUNTER' WHERE username='$USERNAME'" | echo
      fi
    elif [[ $GUESS > $NUMBER ]]
    then
      echo -e "It's lower than that, guess again:\n"
      GUESSES $(( $COUNTER + 1 ))
    else 
      echo -e "It's higher than that, guess again:\n"
      GUESSES $(( $COUNTER + 1 ))
    fi
  else
    echo -e "That is not an integer, guess again:\n"
    GUESSES $COUNTER
  fi
}

GUESSES 1

and here is 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 users;
--
-- Name: users; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE users WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE users OWNER TO freecodecamp;

\connect users

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 (
    user_id integer NOT NULL,
    username character varying(30) NOT NULL,
    games_played integer,
    best_game integer
);


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



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

SELECT pg_catalog.setval('public.users_user_id_seq', 113, true);


--
-- 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);


--
-- PostgreSQL database dump complete
--


Thank you!
Vlad

Please share the specific details of the failing tests.


Here you go

Generally tests are failing, because of difference between the number of guesses and the secret number, and number of guesses test made.

Test is doing that in a bit peculiar way, but the bottom line is - after starting the game it assumes, every line printed out (even if it’s actually empty) means the previous guess was wrong, and test makes another guess.

This means - make sure the game isn’t printing anywhere any not needed blank lines.

Thank you sanity! That worked, I made sure to delete all extra printed lines and the tests passed.

I really feel like that should be a note in the instructions of the test, or change the test to only count actual inputs…

I’d agree it can be confusing and such problem can be very hard to pin-point. Would you be interested in creating issue on the github for this Sign in to GitHub · GitHub?

Sure I can do that, I’ll create an issue right now

1 Like

I am having a similar problem with tests 8 and 13. I feel like I have gotten rid of the empty lines, but it still doesn’t pass, or even gets worse, to where none of the echo/printf command tests pass.

#!/bin/bash

# Connecting to database
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"

# User login/welcome
# echo "Hello, and Welcome to the Number Guessing Game!"
# echo "     (Simple fun for a complex world.)"
echo "Enter your username:"
# echo "(Please limit your name to 22 characters or less)"
read -n 22 username

# Check to see if user has been here before
user_found=$($PSQL "SELECT * FROM users WHERE username = '$username'")

# To be found or not to be found
if [[ -z $user_found ]]; then
  echo "Welcome, $username! It looks like this is your first time here."
else
  # Get player info
  games_played=$($PSQL "SELECT games_played FROM users WHERE username = '$username'")
  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

# Generate number
# Define the range
min=1
max=1000

# Generate a random number within the specified range
random_number=$((RANDOM % (max - min + 1) + min))

# Begin guessing
# Initialize the guess count variable
guess_count=0

# Loop until the user guesses the correct number
while true; do
  echo -e "Guess the secret number between 1 and 1000:"
  read guess

  # Validate the input as an integer
  if ! [[ $guess =~ ^[0-9]+$ ]]; then
    echo -e "That is not an integer, guess again:"
    continue
  fi

  # Increment the guess count
  ((guess_count++))

  if [ $guess -eq $random_number ]; then
    # Congrats
    echo "You guessed it in $guess_count tries. The secret number was $random_number. Nice job!"

    # Check if the current guess count is lower than the best game count
    if [ -z "$best_game" ] || [ $guess_count -lt $best_game ]; then
      # Save the username and guess count as the new best game
      $PSQL "UPDATE users SET best_game = $guess_count WHERE username = '$username';"
      # echo "New best game!"
    else
      # echo "Not a new best game. Keep playing to beat your best score."
    fi

    # Update the number of games played
    $PSQL "UPDATE users SET games_played = games_played + 1 WHERE username = '$username';"

    break
  elif [ $guess -lt $random_number ]; then
    # If number is lower
    echo "It's higher than that, guess again:"
  else
    # If number is higher
    echo "It's lower than that, guess again:"
  fi
done

Add all of your $PSQL commands as a variable. It will stop the extra lines of ‘insert 0 1’ etc from showing up. It counts as a line, as I found out. So something like UPDATE_USER=$($PSQL …

1 Like

Thanks. This helps me to get rid #13 because of output from PSQL