Number Guessing Game Issues (Relational DB)

I’m not passing tests in the “Build A Number Guessing Game” that I would expect to. Starting with the it should start by prompting for a username with " Enter your username:", which my code does do. It then fails most tests that prompt for a follow-up script output. Is there something I’m doing wrong?

Here’s my .sh file:

#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess --tuples-only -c"
NUM=$((1+$RANDOM%1000))
echo "Enter your username:"
read USERNAME
# check if the username is already in the database
USERQ="$($PSQL "SELECT * FROM number_guess WHERE username = '$USERNAME'")"
# if not, welcome them for the first time
if [[ -z $USERQ ]]
then
  echo "Welcome, $USERNAME! It looks like this is your first time here."
# if yes, welcome them back
else
  echo "$USERQ" | while read USER BAR GAMESPL BAR BESTROUND
  do
    echo "Welcome back, $USER! You have played $GAMESPL games, and your best game took $BESTROUND guesses."
  done
fi
# Begin the guessing
WIN=FALSE
ROUND=0
echo "Guess the secret number between 1 and 1000:"
while [ "$WIN" != "TRUE" ]
do
  read GUESS
  # Check if the input is an integer
  if [[ $GUESS =~ ^-?[0-9]+$ ]]
  then
    # if so, check for success
    if (( GUESS==NUM ))
    then
      WIN=TRUE
    else
      # they didn't win, so tell them if it's higher or lower
      if (( GUESS > NUM ))
      then
        echo "It's lower than that, guess again:"
      else
        echo "It's higher than that, guess again:"
      fi
    fi
  # If the input isn't an integer, lambast them for it
  else
    echo "That's not an integer, guess again:"
  fi
  ((++ROUND))
done
# They did, congratulate them and then add a record to the DB
echo "You guessed it in $ROUND tries. The secret number was $NUM. Good job!"
# If they haven't played before, enter their initial record
if [[ -z $USERQ ]]
then
  $INSERT="$($PSQL "INSERT INTO number_guess VALUES ('$USERNAME', 1, $ROUND)")"
# If they have previously played, update their records
else
  echo "$USERQ" | while read USER BAR GAMESPL BAR BESTROUND
  do
    (( GAMESPL++ ))
    if (( ROUND < BESTROUND ))
    then
      $UPDATE="$($PSQL "UPDATE number_guess SET round_count = $ROUND, games_played = $GAMESPL WHERE username = '$USERNAME'")"
    # If it wasn't better, only update games played
    else
      $UPDATE="$($PSQL "UPDATE number_guess SET games_played = $GAMESPL WHERE username = '$USERNAME'")"
    fi
  done
fi

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

And my .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: number_guess; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.number_guess (
    username character varying(22),
    games_played integer,
    round_count integer
);


ALTER TABLE public.number_guess OWNER TO freecodecamp;

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

INSERT INTO public.number_guess VALUES ('Shane', 3, 1);
INSERT INTO public.number_guess VALUES ('user_1667254417078', 5, 66);
INSERT INTO public.number_guess VALUES ('user_1667254486694', 2, 933);
INSERT INTO public.number_guess VALUES ('user_1667254486695', 5, 74);
INSERT INTO public.number_guess VALUES ('user_1667254514181', 2, 486);
INSERT INTO public.number_guess VALUES ('user_1667254514182', 5, 40);
INSERT INTO public.number_guess VALUES ('user_1667254559602', 2, 12);
INSERT INTO public.number_guess VALUES ('user_1667254559603', 5, 134);
INSERT INTO public.number_guess VALUES ('user_1667254589468', 2, 249);
INSERT INTO public.number_guess VALUES ('user_1667254589469', 5, 86);
INSERT INTO public.number_guess VALUES ('user_1667254596696', 2, 262);
INSERT INTO public.number_guess VALUES ('user_1667254596697', 5, 237);
INSERT INTO public.number_guess VALUES ('user_1667254417077', 3, 9);


--
-- PostgreSQL database dump complete
--

I’ve played it couple a times, it seems that every time at the end there are some errors printed.

$ ./number_guess.sh
Enter your username:
not me
Welcome, not me! It looks like this is your first time here.
Guess the secret number between 1 and 1000:
500
It's higher than that, guess again:
750
It's lower than that, guess again:
675
It's higher than that, guess again:
725
It's higher than that, guess again:
735
You guessed it in 5 tries. The secret number was 735. Good job!
./number_guess.sh: line 53: =INSERT 0 1: command not found
$ ./number_guess.sh
Enter your username:
not me
Welcome back, not! You have played | games, and your best game took |           5 guesses.
Guess the secret number between 1 and 1000:
500
It's lower than that, guess again:
250
It's higher than that, guess again:
375
It's lower than that, guess again:
325
It's lower than that, guess again:
280
It's higher than that, guess again:
300
It's lower than that, guess again:
290
It's lower than that, guess again:
285
It's higher than that, guess again:
288
You guessed it in 9 tries. The secret number was 288. Good job!
./number_guess.sh: line 58: ((: |: syntax error: operand expected (error token is "|")
./number_guess.sh: line 59: ((: |           5: syntax error: operand expected (error token is "|           5")
ERROR:  syntax error at or near "WHERE"
LINE 1: UPDATE number_guess SET games_played = | WHERE username = 'n...
                                                 ^
./number_guess.sh: line 64: =: command not found

That’s interesting, I didn’t think to put spaces in my username. It freaks out when that happens it looks like. Thanks for your input!

I’m also getting the:
./number_guess.sh: line 65: =UPDATE 1: command not found
even though, it is actually updating into the DB. Any ideas how that could happen?

Never mind, I figured it out. It did not like declaring my query variable with a “$” at the beginning.

Example: $INSERT=(stuff here) vs. INSERT=(stuff here)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.