Number Guessing Game: opens a site with photos of R. Velasquez

HI.

I’m doing the Number Guessing Game challenge. The verification system tells me that I passed all the requests, except two:

If that username has been used before, it should print Welcome back, ! You have played <games_played> games, and your best game took <best_game> guesses., with 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

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

But I read the correct strings in the terminal, as they are asked for in the user stories: here is a screenshot that demonstrates what I say.

I attach the postgre dump and the bash script:

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.19
-- Dumped by pg_dump version 12.19

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;

CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';

ALTER DATABASE number_guess OWNER TO postgres;

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

CREATE TABLE public.games (
    game_id integer NOT NULL,
    username_id integer NOT NULL,
    guesses integer NOT NULL,
    secret_number integer NOT NULL
);

ALTER TABLE public.games OWNER TO postgres;

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

ALTER TABLE public.games_game_id_seq OWNER TO postgres;

ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;

CREATE TABLE public.usernames (
    username_id integer NOT NULL,
    username character varying(22) NOT NULL
);

ALTER TABLE public.usernames OWNER TO postgres;

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

ALTER TABLE public.usernames_username_id_seq OWNER TO postgres;

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

ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass);

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

INSERT INTO public.games VALUES (1, 1, 10, 584);
INSERT INTO public.games VALUES (2, 1, 8, 238);
INSERT INTO public.games VALUES (3, 1, 11, 773);
INSERT INTO public.games VALUES (4, 2, 5, 125);
INSERT INTO public.games VALUES (5, 2, 7, 994);
INSERT INTO public.games VALUES (6, 2, 9, 863);
INSERT INTO public.games VALUES (7, 3, 7, 25);
INSERT INTO public.games VALUES (8, 3, 8, 997);
INSERT INTO public.games VALUES (9, 4, 3, 620);
INSERT INTO public.games VALUES (10, 4, 4, 425);
INSERT INTO public.games VALUES (11, 3, 7, 891);

INSERT INTO public.usernames VALUES (1, 'Pippo');
INSERT INTO public.usernames VALUES (2, 'Kioana');
INSERT INTO public.usernames VALUES (3, 'Ayako');
INSERT INTO public.usernames VALUES (4, 'Piolo');

SELECT pg_catalog.setval('public.games_game_id_seq', 11, true);

SELECT pg_catalog.setval('public.usernames_username_id_seq', 4, true);

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_pkey PRIMARY KEY (game_id);

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

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

ALTER TABLE ONLY public.games
    ADD CONSTRAINT games_username_id_fkey FOREIGN KEY (username_id) REFERENCES public.usernames(username_id);


--
-- PostgreSQL database dump complete
--

#!/bin/bash

# generate a random number between _1_ and _1000_ for users to guess

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

SECRET_NUMBER=$(( 1 + $RANDOM % 1000 ))
GUESSES=0
username=""

while [[ -z $username ]]
do
  echo "Enter your username:"
  read username
done

# search for '$username' into the database
USERNAME_RESULT=$($PSQL "SELECT username FROM usernames WHERE username = '$username';")

# if it find
if [[ ! -z $USERNAME_RESULT ]]
then
  # it collect the others data
  DATA_RESULT=$($PSQL "
  SELECT COUNT(*), MIN(guesses)
  FROM games AS g
  FULL JOIN usernames AS u
  ON g.username_id = u.username_id
  WHERE u.username = '$USERNAME_RESULT';
  ")
  read GAMES_PLAYED BEST_GAME <<< $(echo $DATA_RESULT | sed 's/[|]/ /g')
  # and say a salutation to the user
  echo "Welcome back, $USERNAME_RESULT! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
else
  # else greet the new user
  echo Welcome, $username! It looks like this is your first time here.
fi

# and start the game
echo Guess the secret number between 1 and 1000:
read GUESS

# check for the $GUESS type (only integer numbers)
while [[ ! $GUESS =~ ^[1-9][0-9]*$ ]]
do
  echo That is not an integer, guess again:
  read GUESS
done

# increment $GUESSES
GUESSES=$(( $GUESSES + 1 ))

# compare $GUESS with $SECRET_NUMBER until GUESS is right
while [[ $GUESS -ne $SECRET_NUMBER ]]
do
  # increment $GUESSES
  GUESSES=$(( $GUESSES + 1 ))
  # check if lower or greater
  if [[ $GUESS -gt $SECRET_NUMBER ]]
  # ask for a new GUESS
  then
    echo It\'s lower than that, guess again:
  else
    echo It\'s higher than that, guess again:
  fi
  read GUESS
done

# right guess: save data into the db
# save username IF NOT PRESENT
if [[ -z $USERNAME_RESULT ]]
then
  SAVE_RESULT=$($PSQL "INSERT INTO usernames (username) VALUES ('$username');")
fi
# get username_id to save the game
USERNAME_ID=$($PSQL "SELECT username_id FROM usernames WHERE username = '$username';")
# save game data
SAVE_RESULT=$($PSQL "INSERT INTO games (username_id, guesses, secret_number) VALUES ($USERNAME_ID, $GUESSES, $SECRET_NUMBER)")
echo You guessed it in $GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!

exit 0

What could be the problem?

Any suggestions will be appreciated.


R. (Rosa) Velasquez is an Argentine actress and singer. She asked to be sent to Mars.

Should the guesses increment happen for each bad guess?

@hbar1st You’re right, the problem is what you indicated.

Thanks for looking at my code.

1 Like