Number Guessing Game help

Hi all,

I’m stuck on the number guessing game project. It seems that the two tests I’m not passing are:

  1. 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

  2. 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

(The final test of being on a clean main branch also isn’t passing because I haven’t got there yet!)

So far I’ve tried:

  • Getting the username from the database rather from the read value.
  • Checking if there are any characters like spaces coming through in the code, but I can’t see any when I test it.
  • Copied and pasted the user story text from the failing tests to make sure I’m not missing anything in it.

My code is below and thanks in advance, any tips much appreciated!

Thanks,
Matt

Here is my database dump:

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

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


ALTER TABLE public.usernames OWNER TO freecodecamp;

--
-- Name: username_username_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.username_username_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.username_username_id_seq OWNER TO freecodecamp;

--
-- Name: username_username_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.username_username_id_seq OWNED BY public.usernames.username_id;


--
-- Name: usernames username_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.usernames ALTER COLUMN username_id SET DEFAULT nextval('public.username_username_id_seq'::regclass);


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



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

SELECT pg_catalog.setval('public.username_username_id_seq', 51, true);


--
-- Name: usernames username_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.usernames
    ADD CONSTRAINT username_pkey PRIMARY KEY (username_id);


--
-- Name: usernames username_username_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.usernames
    ADD CONSTRAINT username_username_key UNIQUE (username);


--
-- PostgreSQL database dump complete
--


Here is my number_guess.sh:

#!/bin/bash

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

#Randomly generate a number 1-1000
NUMBER=$(( ( RANDOM % 1000 ) +1))

#To output the number generated for testing:
  #echo "The number is: $NUMBER".

#Declare number of guesses variable so it's global
NUMBER_OF_GUESSES=0

MAIN_MENU() {
  #Ask for username
  echo -e "\nEnter your username:"
  read USERNAME

  #Search the database for the username given
  USERNAME_SEARCH=$($PSQL "SELECT username FROM usernames WHERE username='$USERNAME'")
  GAMES_PLAYED=$($PSQL "SELECT games_played FROM usernames WHERE username='$USERNAME'")
  BEST_GAME=$($PSQL "SELECT best_game FROM usernames WHERE username='$USERNAME'")

  #If username not in database
  if [[ -z $USERNAME_SEARCH ]]
  then
    #Print "Welcome, <username>! It looks like this is your first time here."
    echo -e "\nWelcome, $USERNAME! It looks like this is your first time here."

    #Input user into database
    USER_INPUT=$($PSQL "INSERT INTO usernames(username) VALUES('$USERNAME')")

  else
  #If user in database
    #Print "Welcome back, <username>! You have played <games_play> games, and your best game took <best_game> guesses."
    echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
  
  fi
  GAME
}

GAME() {
  #Test number of guess variable is working:
    #echo -e "\nYou have had $NUMBER_OF_GUESSES guesses."

#Print "Guess the secret number between 1 and 1000:"
  echo -e "\nGuess the secret number between 1 and 1000:"
  #Read input
  read GUESS

    #If not an integer
    if [[ ! $GUESS =~ [0-9]+ ]]
    then
    #Print "That is not an integer, guess again:"
      echo -e "\nThat is not an integer, guess again:"
      GAME
    #Else
    else
      if [[ $GUESS -gt $NUMBER ]]
      then
      #Increment guess counter
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

      #If user input is higher then print "It's lower than that, guess again:"
        echo -e "\nIt's lower than that, guess again:"

      #Redirect to top of function
        GAME

      elif [[ $GUESS -lt $NUMBER ]]
      then
      #Increment guess counter
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

      #Elif user input is lower then print "It's higher than that, guess again:"
        echo -e "\nIt's higher than that, guess again:"

      #Redirect to top of function
        GAME

      #If the correct number is guessed:
      elif [[ $GUESS -eq $NUMBER ]]
      then
      #Increment guess counter
        NUMBER_OF_GUESSES=$((NUMBER_OF_GUESSES + 1))

      #Update database
        #Increment games played in database for <username>
        INCREMENT_GAME_COUNTER=$($PSQL "UPDATE usernames SET games_played=games_played+1 WHERE username='$USERNAME'")
        
        #Update best game if number of guesses is < best_game figure
        if [[ -z $BEST_GAME ]] || [[ $NUMBER_OF_GUESSES -lt $BEST_GAME ]]
        then
          UPDATE_BEST_GAME=$($PSQL "UPDATE usernames SET best_game=$NUMBER_OF_GUESSES WHERE username='$USERNAME'")
        fi

        #Print "You guesses it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!"
        echo -e "\nYou guessed it in $NUMBER_OF_GUESSES tries. The secret number was $NUMBER. Nice job!"

      fi
    fi
}

MAIN_MENU

Ah doi, I was echoing the “guess the secret number” each time the user got it wrong. Moved this code:

  #Print "Guess the secret number between 1 and 1000:"
  echo -e "\nGuess the secret number between 1 and 1000:"
  GAME

To the bottom of the MAIN_MENU function and they both pass…